I'm writing a python script to copy all Excel files in a tree of directories to another directory. Straightforward, right?
Well, for some reason shutil.copy (or copy2, or copyfile for that matter) don't do anything, not even spit out an error message. Any ideas?
def goFunc(self, event):
print "Starting Go"
for (path, dirs, files) in os.walk(self.path):
print path
print dirs
print files
for every_file in files:
filename = str(path) + str(every_file)
print filename
if filename.endswith('.xlsx'):
print "Copying " + filename + " to " + str(self.path2)
shutil.copyfile(filename, str(self.path2))
print "All DONE!"
So, I added a try except around the copying step, and it seems that it's that step that is the problem:
def goFunc(self, event):
print "Starting Go"
for (path, dirs, files) in os.walk(self.path):
print path
print dirs
print files
for every_file in files:
filename = str(path) +'/' + str(every_file)
print filename
if filename.endswith('.xlsx'):
print "Copying " + filename + " to " + str(self.path2)
try:
shutil.copyfile(filename, str(self.path2))
except:
print "Something went wrong"
pass
print "All DONE!"
Now the output is:
Starting Go
/Users/laptop/Desktop
[u'test']
[u'.DS_Store', u'.localized', u'Maytag.xlsx', u'mer.xlsx']
/Users/laptop/Desktop/.DS_Store
/Users/laptop/Desktop/.localized
/Users/laptop/Desktop/Maytag.xlsx
Copying /Users/laptop/Desktop/Maytag.xlsx to /Users/laptop/test
Something went wrong
/Users/laptop/Desktop/mer.xlsx
Copying /Users/laptop/Desktop/mer.xlsx to /Users/laptop/test
Something went wrong
/Users/laptop/Desktop/test
[]
[]
All DONE!
The files still aren't being copied for some reason.
Solution:
Looks like I needed to add file names to the destinations. Now it works like a charm! Thanks everyone for yout time and help.
def goFunc(self, event):
print "Starting Go"
for (path, dirs, files) in os.walk(self.path):
print path
print dirs
print files
for every_file in files:
filename = str(path) +'/' + str(every_file)
print filename
if filename.endswith('.xlsx'):
print "Copying " + filename + " to " + str(self.path2) + '/' + str(every_file)
try:
shutil.copyfile(filename, str(self.path2)+'/'+ str(every_file))
except:
print "Something went wrong"
pass
print "All DONE!"