gpt4 book ai didi

python - 来自 Python 的 Win32 长路径

转载 作者:可可西里 更新时间:2023-11-01 09:27:44 26 4
gpt4 key购买 nike

注意:这个问题是关于新支持的win32 long paths (自 Windows 10 版本 1607、内部版本 14352 起可用)而不是关于以 \\?\ 开头的扩展 UNC 路径。


我通过组策略启用了长路径支持并重新启动了我的电脑。我在注册表中检查了 HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\FileSystem\LongPathsEnabledHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled 都设置为 1

然后我打开了一个 Python REPL 并尝试创建一个将超过 260 个字符限制的目录,但失败了:

>>> 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 脚本中运行?


更新:我还尝试通过声明为 in its documentatio 的 pywin32 直接调用 Win32 API 函数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 相关。

最佳答案

解决方案既简单又有点令人失望:3.6 之前的 Python 版本无法利用长路径。 3.6 changelist .

Windows historically has limited path lengths to 260 characters. This meant that paths longer than this would not resolve and errors would result.

In the latest versions of Windows, this limitation can be expanded to approximately 32,000 characters. Your administrator will need to activate the “Enable Win32 long paths” group policy, or set the registry value HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled to 1.

This allows the open() function, the os module and most other path functionality to accept and return paths longer than 260 characters when using strings. (Use of bytes as paths is deprecated on Windows, and this feature is not available when using bytes.)

After changing the above option, no further configuration is required.

Changed in version 3.6: Support for long paths was enabled in 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 个字符以内。

关于python - 来自 Python 的 Win32 长路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46488118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com