Python shutil.copytree question

May 11, 2008
20,648
1,171
126
Hi.
There is something i am not sure about.
The shutil.copytree function copies folder and files in a tree.
When looking into I keeps track and gathers all the errors.
But does this mean that shutil.copytree just copies all files that are not raising exceptions ?

I wrote this code to copy the /usr/lib/arm-linux-gnueabihf directory and files to an usb stick.
And at the same time, the symlinks are resolved.
After that from the destination, the duplicate libraries are removed.
This keeps the destination directory small.

Also, how do i retreive the errors that i can print them while keeping the script going instead ?

Code:
import os
import sys
import shutil


print "Python interpreter version"
print (sys.version)
print "Getlib script version 0.1"


libsource_path = "usr/lib/"
usbpathname = raw_input("Please enter the path of the usb stick : ")
print ("Path = " + usbpathname)
print ("Accessing usb stick")
status = os.lstat(usbpathname)
print status
print ("Done.")

destinationpath = usbpathname + "/rpi_warmlibs/arm-linux-gnueabihf/"
print ("Destination path is : " + destinationpath)
print ("Creating directory on usb stick and copying files from /usr/lib/arm-linux-gnueabihf to" + destinationpath)
print "Symlinks will be changed to original files"
print "Please wait..."

try:
    shutil.copytree('/usr/lib/arm-linux-gnueabihf', destinationpath, symlinks=False)
except OSError:
    print (" Few maps or files that already exists where discovered and are probably a duplicate because of the symlink resolving")   
       
print "Done :)"
print "Finished copying"

removepath = destinationpath
print ("Removing double files from : " + removepath)

dirsandfiles = os.listdir(removepath)
for file in dirsandfiles:
    pathandfile = removepath + file
    if os.path.isfile(pathandfile):
        print ("Found file : " + file)
        if not( file.endswith('.a') or file.endswith('.so')):
            print ("Removing file : " + file)
            os.remove(pathandfile)
 
May 11, 2008
20,648
1,171
126
I think this solves it :

Code:
import os
import sys
import shutil


print "Python interpreter version"
print (sys.version)
print "Getlib script version 0.2"


libsource_path = "usr/lib/"
usbpathname = raw_input("Please enter the path of the usb stick : ")
print ("Path = " + usbpathname)
print ("Accessing usb stick")
status = os.lstat(usbpathname)
print status
print ("Done.")

destinationpath = usbpathname + "/rpi_warmlibs/arm-linux-gnueabihf/"
print ("Destination path is : " + destinationpath)
print ("Creating directory on usb stick and copying files from /usr/lib/arm-linux-gnueabihf to" + destinationpath)
print "Symlinks will be changed to original files"
print "Please wait..."

try:
    shutil.copytree('/usr/lib/arm-linux-gnueabihf', destinationpath, symlinks=False)
except OSError as errormessages:
    print ("Some duplicate maps and files where discovered, probably because of the symlink resolvement") 
    print "errors were raised"
    print errormessages
 
print "Done :)"
print "Finished copying"

removepath = destinationpath
print ("Removing double files from : " + removepath)

dirsandfiles = os.listdir(removepath)
for file in dirsandfiles:
    pathandfile = removepath + file
    if os.path.isfile(pathandfile):
        print ("Found file : " + file)
        if not( file.endswith('.a') or file.endswith('.so')):
            print ("Removing file : " + file)
            os.remove(pathandfile)
        else:
            print ("Keeping file : " + file)

I catch the exceptions with :

Code:
except OSError as errormessages:
    print ("Some duplicate maps and files where discovered, probably because of the symlink resolvement")  
    print "errors were raised"
    print errormessages


edit :

I guess not . :(
I have to find a way to keep the script going even when copytree raises errors.
Maybe i should just use the code of copytree and remove the error raising and handle it in my script.
 
Last edited:
May 11, 2008
20,648
1,171
126
I think i finally got it solved. This seems to work. At least no more errors while having duplicate folders.
I have to reraise the error from copytree.

Code:
import os
import sys
import shutil


print "Python interpreter version"
print (sys.version)
print "Getlib script version 0.3"


#libsource_path = "/home/pi/Desktop/test1"
libsource_path = "/usr/lib/arm-linux-gnueabihf"

usbpathname = raw_input("Please enter the path of the usb stick : ")
print ("Path = " + usbpathname)
print ("Accessing usb stick")
status = os.lstat(usbpathname)
print status
print ("Done.")

destinationpath = usbpathname + "/rpi_warmlibs/arm-linux-gnueabihf/"
print ("Destination path is : " + destinationpath)
print ("Creating directory on usb stick and copying files from /usr/lib/arm-linux-gnueabihf to" + destinationpath)
print "Symlinks will be changed to original files"
print "Please wait..."

try:
    shutil.copytree(libsource_path, destinationpath, symlinks=False)
except Exception as errormessages:
    print "Exception occured"
    print ("Some duplicate maps and files where discovered, probably because of the symlink resolvement")   
    print errormessages

except OSError as errormessages:
    print "OSError occured"
    print errormessages
    raise
       
print "Done :)"
print "Finished copying"

removepath = destinationpath
print ("Removing double files from : " + removepath)

dirsandfiles = os.listdir(removepath)
for file in dirsandfiles:
    pathandfile = removepath + file
    if os.path.isfile(pathandfile):
        print ("Found file : " + file)
        if not( file.endswith('.a') or file.endswith('.so')):
            print ("Removing file : " + file)
            os.remove(pathandfile)
        else:
            print ("Keeping file : " + file)