Problem with os.chdir()

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Mar 11 10:34:17 EDT 2009


En Wed, 11 Mar 2009 11:59:57 -0200, venutaurus539 at gmail.com  
<venutaurus539 at gmail.com> escribió:
> On Mar 11, 6:41 pm, Tim Golden <m... at timgolden.me.uk> wrote:
>> venutaurus... at gmail.com wrote:
>> > On Mar 11, 5:19 pm, Tim Golden <m... at timgolden.me.uk> wrote:
>>
>> Well, the source for os.chdir under Windows uses the Win32
>> SetCurrentDirectoryW API as expected. What is not expected
>> is that the MS docs for that function:
>>
>>  http://msdn.microsoft.com/en-us/library/aa365530(VS.85).aspx
>>
>> still seem to suggest that you can't exceed MAX_PATH (ie 260)
>> characters. And indeed, attempting to do a mkdir at the command
>> line of something longer than that will also fail.
>>
>> Hmmm.. maybe the usual advice for naming files \\?\... doesn't
>> apply to directory paths?
>>
>> Do you have an already existing full pathname that long?
>
>       My application demands me to create deep paths of (1023) long.
> I've cross checked it and the folder actually exists.

As TJG said, it appears that you can create such deep path, and create and  
use files inside, but you can't chdir into it:

py> cien = '0123456789'*10
py> path = ur"\\?\c:\%s\%s\%s\%s\%s\%s\%s\%s\%s\%s\%s\%s" % ((cien,)*12)
py> len(path)
1218
py> os.mkdir(path) # after creating all intermediate directories
py> fn = os.path.join(path, 'x.txt')
py> f = open(fn, "w")
py> f.write("hello")
py> f.close()
py> open(fn).read()
'hello'
py> os.chdir(path)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
WindowsError: [Error 123] El nombre de archivo, directorio o etiqueta del  
volume
n no es vßlido:  
u'\\\\?\\c:\\012345678901234567890123456789012345678901234567890..."

So your application should always use absolute paths.

-- 
Gabriel Genellina




More information about the Python-list mailing list