在Python中将具有太长路径的文件复制到另一个目录 [英] Copy a file with a too long path to another directory in Python
问题描述
shutil.copyfile(copy_file, dest_file)
我得到以下IOError:
[Errno 2]没有这样的文件或目录
该文件确实存在!问题是文件的路径太长。 (> 255个字符)
如何复制这些文件?在其他应用程序中打开它们不是问题。
要创建一个路径过长的文件,请创建一个尽可能长的文件名,然后将包含的文件夹沿树结构向下移动。
我一直在尝试其中的一些方法没有成功:
http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html
我不确定255字符限制,所以我偶然发现了此帖。我找到一个工作答案:在路径之前添加\\?\。
shutil.copyfile \\\?\\+ copy_file,dest_file)
编辑:
我发现使用长路径会导致Windows上的问题。我使用的另一个技巧是只是缩短路径:
import win32api
path = win32api.GetShortPathName(path)
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've been trying some of these methods without success: http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html
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)
这篇关于在Python中将具有太长路径的文件复制到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!