0
votes

I have a folder which has 586 excel files with .xls and .xlsx format, then I use shutil and I transfer the files to destination like the code below.

import shutil
import glob

for filename in glob.glob('C:\\Users\\Documents\\sample_folder\\**\\**', recursive=True):
    if filename.endswith('.xlsx') or filename.endswith('.xls'):
    shutil.copy(filename,"C:\\Users\\Documents\\excel-files")

Things work well and 398 files get transferred to destination successfully but I don't get the remaining files and it shows a error like

File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 121, in copyfile with open(dst, 'wb') as fdst: PermissionError: [Errno 13] Permission denied: 'C:\Users\Documents\excel-files\XYZ 1310_template.xlsx'

2
Do you have XYZ 1310_template.xlsx open somewhere?DavidG
@DavidG, nope I dontuser8521874

2 Answers

1
votes

I am pretty much sure that your shutil.(part of the code is not the right approach).

Your just trying to copy the filename which is a variable and has names of the files in it.

whereas shutil.copy("src","dest") is required src, is from the source not the filename there.

  shutil.copy("src"+filename,"C:\\Users\\Documents\\excel-files")
1
votes

Consider using shutil.copyfile(src, dst) instead of shutil.copy(), since you have permission to write all files. Let me know if this works.

Otherwise, check if you have permission to write in the specified file, try to run the program without the XYZ 1310_template.xlsx file and see if this is the only ile that doesn't work.

Some more tips can be found in the documentary or that post asked earlier.