注意:这个问题是关于新支持的win32 long paths(自Windows 10版本1607,Build 14352以来可用),而不是关于以\\?\
开头的扩展UNC路径。
我通过组策略启用了长路径支持并重新启动了我的PC。我在注册表中检查了HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\FileSystem\LongPathsEnabled
和HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled
都设置为1
。
然后我打开了一个Python REPL并尝试创建一个超过260-char限制的目录,但是失败了:
>>> import os
>>> longdirname = 'a' * 300
>>> longdirname
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
>>> os.makedirs(longdirname)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python27\lib\os.py", line 157, in makedirs
mkdir(name, mode)
WindowsError: [Error 3] The system cannot find the path specified: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
[Error 3] The system cannot find the path specified: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
我假设长路径支持在某种程度上没有在这里生效。为什么这样,我怎样才能正确启用它以便它可以从Python脚本中运行?
更新:我还尝试通过pywin32直接调用Win32 API函数,声称in its documentatio n它应该支持长路径,但是再次失败:
>>> import win32file
>>> longname = 'a' * 300
>>> win32file.CreateDirectoryW(longname, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
error: (3, 'CreateDirectoryW', 'The system cannot find the path specified.')
(3, 'CreateDirectoryW', 'The system cannot find the path specified.')
更新2:还尝试通过单个组件创建:
>>> for i in range(1, 300):
... win32file.CreateDirectoryW('a', None)
... os.chdir('a')
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
error: (206, 'CreateDirectoryW', 'The filename or extension is too long.')
(206, 'CreateDirectoryW', 'The filename or extension is too long.')
(os.makedirs
在这种情况下也不会工作)
更新3:以下批处理脚本将不失败:
@echo off
setlocal enableextensions enabledelayedexpansion
pushd
set /a count = 1
for /L %%i in (1,1,300) do (
mkdir a
cd a
echo %%i
)
endlocal
popd
所以我假设这与Python有某种关系。
答案 0 :(得分:2)
解决方案既简单又有些令人失望:3.6之前的Python版本无法利用长路径。 3.6 changelist
Windows历史上有限的路径长度为260个字符。这意味着长于此的路径将无法解决,并且会导致错误。
在最新版本的Windows中,此限制可以扩展到大约32,000个字符。您的管理员需要激活“启用Win32长路径”组策略,或将注册表值HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ FileSystem @ LongPathsEnabled设置为1.
这允许open()函数,os模块和大多数其他路径功能在使用字符串时接受并返回长度超过260个字符的路径。 (在Windows上不推荐使用字节作为路径,并且在使用字节时此功能不可用。)
更改上述选项后,无需进一步配置。
版本3.6中更改:在Python中启用了对长路径的支持。
为了证明它有效,我做了以下事情:
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> longdir = 'a' * 300
>>> os.makedirs(longdir)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\tamas\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 220, in makedirs
mkdir(name, mode)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
>>> for i in range(1, 300):
... os.mkdir('a')
... os.chdir('a')
...
>>> len(os.getcwd())
631
>>>
第一个makedirs
调用失败,因为单个组件仍限制为255个字符。