Wolph

Windows version of os.walk with long path (255 characters) support

There are these times that you really have to use Windows and in those cases some issues like the 255 path limit tend to occur with network mounts.

Luckily, this is fairly easy to solve with a small Python snippet:

[python]import os

UNUSED_DRIVE_LETTER = ‘x:’

def windows_walk(top, topdown=True, onerror=None, followlinks=False):
for path, dirs, files in os.walk(top):
os.system(‘subst “%s” “%s”‘ % (UNUSED_DRIVE_LETTER, path))
yield UNUSED_DRIVE_LETTER, dirs, files
os.system(‘subst -d “%s”‘ % UNUSED_DRIVE_LETTER)
[/python]

Just use it like the normal `os.walk`, so something like this:

[python]
for path, dirs, files in windows_walk(‘some_long_path’):
print ‘%s takes %s bytes in %s files’ % (
path,
sum([getsize(join(path, name)) for name in files]),
len(files),
)
[/python]

Exit mobile version