Problem with os.chdir()

Tim Golden mail at timgolden.me.uk
Wed Mar 11 10:17:17 EDT 2009


venutaurus539 at gmail.com wrote:
> 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:
>>>>> Here is my code snippet which you will be interested in:
>>>> Indeed.
>>>>> file = ur'\\?\C:\\TestDataSet\DeepPaths
>>>>> \DeepPathLevel01\DeepPathLevel02\DeepPathLevel03\DeepPathLevel04\DeepPathLe vel05\DeepPathLevel06\DeepPathLevel07\DeepPathLevel08\DeepPathLevel09\DeepP athLevel10\DeepPathLevel11\DeepPathLevel12\DeepPathLevel13\DeepPathLevel14\ DeepPathLevel15\DeepPathLevel16\DeepPathLevel172.txt'
>>>> And what happens if you remove that second double-backslash,
>>>> the one between C: and TestDataSet?
>>>> TJG
>>> --------------------------------------------------------------------------- -------------
>>> Even if I give the file path as below
>>> file = ur'\\?\C:\TestDataSet\DeepPaths
>>> \DeepPathLevel01\DeepPathLevel02\DeepPathLevel03\DeepPathLevel04\DeepPathLe vel05\DeepPathLevel06\DeepPathLevel07\DeepPathLevel08\DeepPathLevel09\DeepP athLevel10\DeepPathLevel11\DeepPathLevel12\DeepPathLevel13\DeepPathLevel14\ DeepPathLevel15\DeepPathLevel16\DeepPathLevel172.txt'
>>> I am still getting the exception:
>>> Traceback (most recent call last):
>>>   File "C:\JPDump\test.py", line 29, in <module>
>>>     renameStubs(file)
>>>   File "C:\JPDump\test.py", line 12, in renameStubs
>>>     os.chdir (path)
>>> WindowsError: [Error 206] The filename or extension is too long: u'\\\
>>> \?\\C:\\TestDataSet\\DeepPaths\\DeepPathLevel01\\DeepPathLevel02\
>>> \DeepPathLevel03\\DeepPathLevel04\\DeepPathLevel05\\DeepPathLevel06\
>>> \DeepPathLevel07\\DeepPathLevel08\\DeepPathLevel09\\DeepPathLevel10\
>>> \DeepPathLevel11\\DeepPathLevel12\\DeepPathLevel13\\DeepPathLevel14\
>>> \DeepPathLevel15\\DeepPathLevel16\\'
>> 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?
>>
>> TJG
> 
> Yes Sir,
>       My application demands me to create deep paths of (1023) long.
> I've cross checked it and the folder actually exists.


Well, a little bit of experimentation shows that you can
*create* paths this deep (say, with os.mkdir). But you
can't actually set the current directory to it. So the
next question is: do you actually need to be *in* that
directory, rather than simply to reference it?

In other words, you can do this (assuming you have a c:\temp):

<code>
import os
for i in range (1, 15):
  os.mkdir (ur"\\?\c:\temp\%s" % "\\".join (100 * "c" for j in range (i)))

</code>

But you can't then os.chdir to it. You're hitting the limits of
the OS. Try accessing files directly within the structure
you're using. (ie without chdir-ing there first).

TJG



More information about the Python-list mailing list