windows python win32文件名长度解决方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3555527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python win32 filename length workaround
提问by user246456
I have found out that you can't open(filepath)
when filepath length is greater than 255 characters even if the filename itself is 10 characters long (the remaining part is the directory path).
我发现open(filepath)
即使文件名本身长度为 10 个字符(其余部分是目录路径),当文件路径长度大于 255 个字符时也不能。
Any idea to work around this issue? (python 2.6 on win32)
有什么想法可以解决这个问题吗?(win32 上的 python 2.6)
回答by Luke
The most general approach to this is to prefix the path with \\\\?\\
(reference). Be aware that this disables certain pre-processing on the path, but nothing major IMO.
对此最通用的方法是在路径前加上\\\\?\\
( reference)前缀。请注意,这会禁用路径上的某些预处理,但不是主要的 IMO。
Also I can note that on 32-bit Windows Server 2003 with Python 2.7 I had to use prefixed Unicode path (u"\\\\\\\\?\\\\"
prefix or ur"\\\\?\\"
) since (as mentioned in reference) non-Unicode API functions may still be limited to MAX_PATH
length even though the prefix is used.
我还可以注意到,在带有 Python 2.7 的 32 位 Windows Server 2003 上,我必须使用带前缀的 Unicode 路径(u"\\\\\\\\?\\\\"
前缀或ur"\\\\?\\"
),因为(如参考中所述)非 Unicode API 函数可能仍被限制为MAX_PATH
长度,即使使用了前缀.
e.g., ur"\\\\?\\c:\temp\....\abc.txt"
例如, ur"\\\\?\\c:\temp\....\abc.txt"
回答by PaulMcG
A Windows OS level solution is to use the DOS SUBST command to define a pseudo drive at a particular directory.
Windows 操作系统级别的解决方案是使用 DOS SUBST 命令在特定目录中定义伪驱动器。
SUBST Q: C:\really\long\path\name\full\of\sub\directories
Then you can access the files in that directory as Q:filename
.
然后您可以访问该目录中的文件Q:filename
。