I am trying to copy files on Windows with Python 2.7, but sometimes this fails.
shutil.copyfile(copy_file, dest_file)
I get the following IOError:
[Errno 2] No such file or directory
But the file does exist! The problem is that the path of the file is too long. (> 255 characters)
How do I copy these files? It isn't a problem to open them in other applications.
To create a file with a too long path, create a file with an as long as possible file name and move the containing folder deeper down a tree structure.
解决方案
I wasn't sure about the 255 char limit so I stumbled on this post. There I found a working answer: adding \\?\ before the path.
shutil.copyfile("\\\\?\\" + copy_file, dest_file)
edit:
I've found that working with long paths causes issues on Windows. Another trick I use is to just shorten the paths:
import win32api
path = win32api.GetShortPathName(path)