WindowsError:[错误3]系统找不到指定的路径(当路径太长?)(WindowsError: [Error 3] The system cannot find the path specifi

编程入门 行业动态 更新时间:2024-10-27 14:24:50
WindowsError:[错误3]系统找不到指定的路径(当路径太长?)(WindowsError: [Error 3] The system cannot find the path specified (when path too long?))

WindowsError:[错误3]系统找不到指定的路径(当路径太长?)

我正在创建一个脚本来查找两个目录之间的唯一文件。 为了做到这一点,我使用os.walk()来遍历文件,如果存在相同大小的文件,我将它们哈希以确保它们是相同的(打开过程中的文件)。 问题是某些文件在打开时会产生上述错误。 人们遇到此问题的最常见原因是因为路径未正确连接,从而导致脚本尝试打开不存在的文件。 对我来说情况并非如此。

尝试不同的目录组合后,我开始注意到一种模式,即生成错误的文件似乎具有深层目录结构和长文件名。 我想不出这个问题的任何其他原因 - 没有字符编码错误(我将所有路径解码为UTF-8),并且路径确实存在,因为os.walk() 。

我的行走代码:

for root, dirs, files in os.walk(directory): for filename in files: file_path = os.path.join(root, filename)

我的哈希码:

def hash(file_path): with open(dir_file, 'rb') as f: hasher = hashlib.md5() while True: buf = f.read(byte_size) if buf != '': hasher.update(buf) else: break result = hasher.hexdigest() return result

编辑:问题出现的最新路径是5个目录深(包含142个字符,占双反斜杠),文件名是另外122个字符长

WindowsError: [Error 3] The system cannot find the path specified (when path too long?)

I'm making a script to find unique files between two directories. In order to do this, I use os.walk() to walk through the files, and if files of the same size exist, I hash them to ensure they are the same (opening the files in the process). The problem is that some files produce the above-mentioned error when opened. The most common reason people run into this problem is because the path is not correctly joined, thereby causing the script to try and open a file that doesn't exist. This isn't the case for me.

After trying different combinations of directories, I began to notice a pattern whereby files that produce the error seem to have a deep directory structure and a long filename. I can't think of any other reason for the issue - there are no character-encoding errors (I decode all my paths to UTF-8) and the paths do exist by virtue of os.walk().

My walk code:

for root, dirs, files in os.walk(directory): for filename in files: file_path = os.path.join(root, filename)

My hashing code:

def hash(file_path): with open(dir_file, 'rb') as f: hasher = hashlib.md5() while True: buf = f.read(byte_size) if buf != '': hasher.update(buf) else: break result = hasher.hexdigest() return result

Edit: The most recent path where the issue appeared was 5 directories deep (containing 142 characters, accounting for double backslashes), and the filename was an additional 122 characters long

最满意答案

这是由于MSDN上解释的Windows API文件路径大小限制:

在Windows API中(以下段落中讨论了一些例外),路径的最大长度为MAX_PATH, 定义为260个字符 。 本地路径按以下顺序构成:驱动器号,冒号,反斜杠,由反斜杠分隔的名称组件以及终止空字符。 例如,驱动器D上的最大路径是“D:\某些256个字符的路径字符串”,其中“”表示当前系统代码页的不可见的终止空字符。 (这里使用字符<>是为了清晰可见,并且不能成为有效路径字符串的一部分。)

正如在该页面上所解释的那样,较新版本的Windows支持用于Unicode路径等的扩展文件路径前缀( \\?\ ),但这不是一致或保证的行为,即它并不意味着它将在所有情况下都有效。

无论哪种方式,尝试使用扩展路径前缀预先添加路径,看看它是否适用于您的情况:

file_path = "\\\\?\\" + os.path.join(root, filename)

That's due to Windows API file path size limitation as explained on MSDN:

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

As also explained on that page, newer versions of Windows support extended file path prefix (\\?\) used for Unicode paths and such, but that's not a consistent or guaranteed behavior i.e. it doesn't mean it will work in all cases.

Either way, try prepending your path with the extended path prefix and see if it works for your case:

file_path = "\\\\?\\" + os.path.join(root, filename)

更多推荐

本文发布于:2023-08-07 12:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464182.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   找不到   太长   错误   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!