I am trying to copy specific files from one folder to another but it raise me the error: IOError: [Errno 2] No such file or directory: 'AERIAL_P.dbf' altought the file exists in the main folder. I attached a picture with my code. enter image description here
How can I resolve this error. Thank you
CodePudding user response:
os.listdir gives the names of the files in a directory : you do not have the directory name; you have to add it to file name begire using it in shutil.copy
CodePudding user response:
Seems like you are not creating your target folder.
Replace:
if not os.path.exists(target):
os.makedirs(target)
With:
if not os.path.exists(target_folder):
os.makedirs(target_folder)
Next to that you are only using the name of the file and not the full path. Use the full path.
Replace:
shutil.copy(items, target_folder)
With
shutil.copy(src_files '/' items, target_folder)