4

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') 
Óscar López
  • 232,561
  • 37
  • 312
  • 386
user2646309
  • 53
  • 1
  • 4
  • 2
    I wonder what operating system lets you have name files that long. – PepperoniPizza Aug 02 '13 at 15:04
  • 4
    Can you provide us with some more details? What os are you using, and can you show us a minimal version of your code that demonstrates the problem? – larsks Aug 02 '13 at 15:05
  • windows, code simple, no any special: for root,dirs,files in os.walk(...): print root . I have tested, if rename the long-name files with short names, code can get correct result. – user2646309 Aug 02 '13 at 15:20
  • I'm just reiterating what @larsks said, we need to see a minimal version of your code to help diagnose your problem! – Hooked Aug 02 '13 at 15:25

3 Answers3

5

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.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

The only workaround I can think of is to map the the folder to the specific directory. This will make the path way shorter. e.g. z:\myfile.xlsx instead of c:\a\b\c\d\e\f\g\myfile.xlsx

Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29
0

The limitation for maximum length for a path is 256. Starting in Windows 10, version 1607, this limitation have been removed, but you have to opt-in to this new behavior. You can either do one of these:

  1. Open registry, navigate to Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\, and create a new key with the name LongPathsEnabled, type REG_DWORD and value 1. 2

  2. In PowerShell command New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force

Read more about how Windows naming files, paths and namespaces here.

Or you can run a virtual machine using an Unix OS.

Ooker
  • 1,969
  • 4
  • 28
  • 58