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]

Bookmark and Share

Tags:

About Rick van Hattem

Rick van Hattem is a Dutch Internet entrepreneur and co-founder of Fashiolista.com

One Response to “Windows version of os.walk with long path (255 characters) support”

  1. alina9 | 2015-10-24 at 22:50:07 #

    Long Path Tool is a best software to solve this issue. 5++ Rating. Long Path Tool is a best software
    it is a free software. You can Try it

Leave a Reply