开发者

python win32 filename length workaround

I have found out that you can't开发者_如何学Go 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).

Any idea to work around this issue? (python 2.6 on win32)


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.

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.

e.g., ur"\\\\?\\c:\temp\....\abc.txt"


A Windows OS level solution is to use the DOS SUBST command to define a pseudo drive at a particular directory.

SUBST Q: C:\really\long\path\name\full\of\sub\directories

Then you can access the files in that directory as Q:filename.


From Windows 10, version 1607. The limitation can be disabled by modifiying the registry key Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled and setting it to 1.

Powershell command to enable long paths

New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
-Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force

Windows documentation for reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜