1

The problem is the character limit for the path in windows when creating multiple directories using pythons os.makedirs()

I found this post when searching for my problem before posting this:

python win32 filename length workaround

Now the chosen answer suggests the prefix workaround but my question here is, is there a way to ensure functionality in Windows and UNIX?

The other approach I thought of was to create the folders one by one and then create the file so that you never exceed the path length, but I can't figure out the obvious bug in the code.

path = ['folder1/s1/s1/abc.txt',
        'folder1/s1/s2/def.txt']

def makedirs(path):
    explode = path.split('/')
    for i in range(len(explode)-1):
        os.mkdir(explode[i])
        os.chdir(explode[i])

        if i == len(explode) -2:
            download_file(explode[i+1])

    # something to go back here
    os.chdir('../' * (len(explode)-3)) # ??


makedirs(path[0])

Now this works for only the first line because I can't figure out how to get back to the root or reset it. Without the 'reset' the folders are being under each other:

folder1/s1/s1/folder1/s1/s1/abc.txt (or something like that)

I could set the path from root to reset it but then we might run into the same issue of reaching the max length. Any help on how to get this working on both OS would be appreciated!

Please feel free to point out where I'm wrong.

Community
  • 1
  • 1
Kartik
  • 9,463
  • 9
  • 48
  • 52

2 Answers2

2

you need to use unc path and unicode filenames, but not all python functions are aware of this, os.mkdir works while os.makedirs not

import os

path = u'\\\\?\\c:\\'

for i in xrange(1000):
    path += u'subdir\\'
    os.mkdir(path)

but it's better to give also the code to remove them, windows explorer is unable to delete

import os

path = u'\\\\?\\c:\\'

for i in xrange(1000, 0, -1):
    try:
        os.rmdir(path + (u'subdir\\' * i))
    except:
        pass
sherpya
  • 4,890
  • 2
  • 34
  • 50
1

Per this stackoverflow answer: while chdir can go up one directory with os.chdir(".."), the platform-agnostic way is: os.chdir(os.pardir).

Either call this N times in a loop;
or try an unreadable one-liner like this (untested):
os.chdir(os.path.join(*([os.pardir] * NUM_TIMES)))

(Instead of path.split('/'), you could also use the method described here for it to work on all operating systems)

Community
  • 1
  • 1
HugoRune
  • 13,157
  • 7
  • 69
  • 144
  • This works! turns out it was just my math, that was wrong. `(len(explode)-1)` was the fix. But I switched it to use `os.pardir` instead. – Kartik Aug 19 '12 at 01:54