I have a script running that at a certain stage has to move a lot of files from an internal drive to a network drive using:
for filename in os.listdir(finished_folder):
source_file_path = os.path.join(finished_folder, filename)
shutil.move(source_file_path, destination_file_path)
This goes well 99% of the time. I am transferring many thousands of files per day but very occasionally it gives the following error:
05/18/2024 10:37:02 AM Uncaught exception
Traceback (most recent call last):
File “C:UsersnlvxxxxxAppDataLocalProgramsPythonPython311Libshutil.py”, line 825, in move
os.rename(src, real_dst)
OSError: [WinError 17] The system cannot move the file to a different drive: ‘C:UsersnlvxxxxxDesktopfile.png’ -> ‘\code1storageTLdata_Projectsfile.png’During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:UsersnlvxxxxxDesktopscript.py”, line 552, in
shutil.move(source_file_path, destination_file_path)
File “C:UsersnlvxxxxxAppDataLocalProgramsPythonPython311Libshutil.py”, line 846, in move
os.unlink(src)
PermissionError: [WinError 32] The process does not have access to the file because it is being used by another process: ‘C:Usersnlv22087Desktopfile.png’
When I look at the source and file destination after the error, the file exists on both locations. So it has copied it but then can not proceed for some reason due to the above mentioned error.
Did not try anything else to solve it, I can fix it by a try, exception block if there are no other known solutions, this will probably work, but would like to have other thoughts before implementing it