How To/python/

Using python 2.5

Playing with long paths on Windows

I have very long path on windows and i get error when try to get modification time. So i tried do chdir path and then get file. It now gives me error that file doesn't exists.

I know there is a win32 module but i can't find any documentation for it (what is the purpose to create app without docs?). Any idea?

I searched google but there where only 2 options. Use chdir (not working) or use win32api (where is no documentation).

os.chdir('very_long_path')
# works
 
os.listdir('very_long_path')
# gives:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: listdir() argument 1 must be (buffer overflow), not str
 
os.chdir('very_long_path')
os.listdir('.')
#works
 
os.chdir('very_long_path')
os.path.getmtime(os.listdir('.')[0])
#throws exception (path not exists)
 
os.chdir('very_long_path')
open(os.listdir('.')[0])
#throws exception (path not exists)

Solutions

solution 1

I dont have a solution but workaround. I can map long path as drive:

longPath = "c:\\documents and settings\\usermth\\my documents\\......blablablabla"
 
# map path as drive b: (notice "" around path to avoid problems with spaces)
os.system('subst b: "%s"' % longPath)
 
longPath = 'b:\\'
 
to remove mapping use:
os.system('subst b: /D')

solution 2

You can also use \\?\ prefix but ONLY FOR unicode strings.

os.path.getmtime("\\\\?\\c:\\very long path\\blablabla") #will not work
os.path.getmtime(u"\\\\?\\c:\\very long path\\blablabla") #will work

Note that path has to be absolute and normalized (cannot contains . or ..). You can use @os.path.normpath(myPath)@.