Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'. I tried to use yield, but also get nothing and shows 'Traceback: StopIteration'.

OS is windows, code is simple. 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') 
like image 387
user2646309 Avatar asked Oct 24 '25 19:10

user2646309


1 Answers

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. See this post for more details.

like image 194
Óscar López Avatar answered Oct 26 '25 08:10

Óscar López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!