[英]Python's os.walk() fails in Windows when there are long filenames
I use python os.walk() to get files and dirs in some directories, but there're files whose names are too long(>300), os.walk() return nothing, use onerror I get '[Error 234] More data is available'.我使用 python os.walk() 来获取某些目录中的文件和目录,但是有些文件的名称太长(>300),os.walk() 什么也不返回,使用 onerror 我得到 '[Error 234] More数据可用'。 I tried to use yield, but also get nothing and shows 'Traceback: StopIteration'.
我尝试使用yield,但也一无所获并显示'Traceback:StopIteration'。
OS is windows, code is simple.操作系统是windows,代码很简单。 I have tested with a directory, if there's long-name file, problem occur, while if rename the long-name files with short names, code can get correct result.
我用一个目录测试过,如果有长名文件,就会出现问题,而如果用短名重命名长名文件,代码可以得到正确的结果。
I can do nothing for these directories, such as rename or move the long-name files.我不能对这些目录做任何事情,例如重命名或移动长名称文件。 Please help me to solve the problem!
请帮我解决问题!
def t(a):
for root,dirs,files in os.walk(a):
print root,dirs,files
t('c:/test/1')
In Windows file names (including path) can not be greater than 255 characters, so the error you're seeing comes from Windows, not from Python - because somehow you managed to create such big file names, but now you can't read them. 在Windows中,文件名(包括路径)不能超过255个字符,因此您看到的错误来自Windows,而不是来自Python - 因为不知何故,您设法创建了如此大的文件名,但现在您无法读取它们。 See this post for more details.
有关详细信息,请参阅此帖子 。
The only workaround I can think of is to map the the folder to the specific directory.我能想到的唯一解决方法是将文件夹映射到特定目录。 This will make the path way shorter.
这将使路径更短。 eg z:\\myfile.xlsx instead of c:\\a\\b\\c\\d\\e\\f\\g\\myfile.xlsx
例如 z:\\myfile.xlsx 而不是 c:\\a\\b\\c\\d\\e\\f\\g\\myfile.xlsx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.