Problem with os.chdir()

Discussion in 'Python' started by venutaurus539@gmail.com, Mar 11, 2009.

  1. Guest

    Hello all,
    I am writing a python script which has to access deep paths
    then supported normally by the Windows OS (>255). So I am appending "\
    \?\" to do so. But when I use the path in the above fashion with
    os.chdir() it is unable to recognize my folder and throwing an error:

    Traceback (most recent call last):
    File "C:\JPDump\test.py", line 31, in <module>
    renameStubs(file)
    File "C:\JPDump\test.py", line 15, in renameStubs
    os.chdir (path)
    WindowsError: [Error 123] The filename, directory name, or volume
    label syntax is incorrect: '\\?\\C:\\TestDataSet\
    \Many_Files_10000_1KB_FIles\\001_0009_1000 FILES\\'

    The value of my path variable is
    \?\C:\TestDataSet\Many_Files_10000_1KB_FIles\001_0009_1000 FILES\

    Can someone please help me in this..

    Thank you,
    Venu M
    , Mar 11, 2009
    #1
    1. Advertising

  2. Tim Golden Guest

    wrote:
    > Hello all,
    > I am writing a python script which has to access deep paths
    > then supported normally by the Windows OS (>255). So I am appending "\
    > \?\" to do so. But when I use the path in the above fashion with
    > os.chdir() it is unable to recognize my folder and throwing an error:
    >
    > Traceback (most recent call last):
    > File "C:\JPDump\test.py", line 31, in <module>
    > renameStubs(file)
    > File "C:\JPDump\test.py", line 15, in renameStubs
    > os.chdir (path)
    > WindowsError: [Error 123] The filename, directory name, or volume
    > label syntax is incorrect: '\\?\\C:\\TestDataSet\
    > \Many_Files_10000_1KB_FIles\\001_0009_1000 FILES\\'
    >
    > The value of my path variable is
    > \?\C:\TestDataSet\Many_Files_10000_1KB_FIles\001_0009_1000 FILES\



    There need to be two backslashes at the beginning:

    \\?\C:\TEST.....FILES\

    Note the double backslash before the question mark.

    TJG
    Tim Golden, Mar 11, 2009
    #2
    1. Advertising

  3. Guest

    On Mar 11, 11:08 am, Tim Golden <> wrote:
    > wrote:
    > > Hello all,
    > >            I am writing a python script which has to access deep paths
    > > then supported normally by the Windows OS (>255). So I am appending "\
    > > \?\" to do so. But when I use the path in the above fashion with
    > > os.chdir() it is unable to recognize my folder and throwing an error:

    >
    > > Traceback (most recent call last):
    > >   File "C:\JPDump\test.py", line 31, in <module>
    > >     renameStubs(file)
    > >   File "C:\JPDump\test.py", line 15, in renameStubs
    > >     os.chdir (path)
    > > WindowsError: [Error 123] The filename, directory name, or volume
    > > label syntax is incorrect: '\\?\\C:\\TestDataSet\
    > > \Many_Files_10000_1KB_FIles\\001_0009_1000 FILES\\'

    >
    > > The value of my path variable is
    > > \?\C:\TestDataSet\Many_Files_10000_1KB_FIles\001_0009_1000 FILES\

    >
    > There need to be two backslashes at the beginning:
    >
    > \\?\C:\TEST.....FILES\
    >
    > Note the double backslash before the question mark.
    >
    > TJG


    I've another situation where os.chdir() function failed. Please find
    the traceback pasted below:
    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: '\\?\
    \C:\\TestDataSet\\DeepPaths\\DeepPathLevel01\\DeepPathLevel02\
    \DeepPathLevel03\\DeepPathLevel04\\DeepPathLevel05\\DeepPathLevel06\
    \DeepPathLevel07\\DeepPathLevel08\\DeepPathLevel09\\DeepPathLevel10\
    \DeepPathLevel11\\DeepPathLevel12\\DeepPathLevel13\\DeepPathLevel14\
    \DeepPathLevel15\\DeepPathLevel16\\'

    Thanks in advance,
    Venu Madhav.
    , Mar 11, 2009
    #3
  4. Tim Golden Guest

    wrote:
    > On Mar 11, 11:08 am, Tim Golden <> wrote:
    >> wrote:
    >>> Hello all,
    >>> I am writing a python script which has to access deep paths
    >>> then supported normally by the Windows OS (>255). So I am appending "\
    >>> \?\" to do so. But when I use the path in the above fashion with
    >>> os.chdir() it is unable to recognize my folder and throwing an error:
    >>> Traceback (most recent call last):
    >>> File "C:\JPDump\test.py", line 31, in <module>
    >>> renameStubs(file)
    >>> File "C:\JPDump\test.py", line 15, in renameStubs
    >>> os.chdir (path)
    >>> WindowsError: [Error 123] The filename, directory name, or volume
    >>> label syntax is incorrect: '\\?\\C:\\TestDataSet\
    >>> \Many_Files_10000_1KB_FIles\\001_0009_1000 FILES\\'
    >>> The value of my path variable is
    >>> \?\C:\TestDataSet\Many_Files_10000_1KB_FIles\001_0009_1000 FILES\

    >> There need to be two backslashes at the beginning:
    >>
    >> \\?\C:\TEST.....FILES\
    >>
    >> Note the double backslash before the question mark.
    >>
    >> TJG

    >
    > I've another situation where os.chdir() function failed. Please find
    > the traceback pasted below:
    > 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: '\\?\
    > \C:\\TestDataSet\\DeepPaths\\DeepPathLevel01\\DeepPathLevel02\
    > \DeepPathLevel03\\DeepPathLevel04\\DeepPathLevel05\\DeepPathLevel06\
    > \DeepPathLevel07\\DeepPathLevel08\\DeepPathLevel09\\DeepPathLevel10\
    > \DeepPathLevel11\\DeepPathLevel12\\DeepPathLevel13\\DeepPathLevel14\
    > \DeepPathLevel15\\DeepPathLevel16\\'


    Try it as a unicode string:

    os.chdir (ur"\\?\c:\test...\deep...")

    TJG
    Tim Golden, Mar 11, 2009
    #4
  5. Guest

    On Mar 11, 5:02 pm, Tim Golden <> wrote:
    > wrote:
    > > On Mar 11, 11:08 am, Tim Golden <> wrote:
    > >> wrote:
    > >>> Hello all,
    > >>>            I am writing a python script which has to access deep paths
    > >>> then supported normally by the Windows OS (>255). So I am appending "\
    > >>> \?\" to do so. But when I use the path in the above fashion with
    > >>> os.chdir() it is unable to recognize my folder and throwing an error:
    > >>> Traceback (most recent call last):
    > >>>   File "C:\JPDump\test.py", line 31, in <module>
    > >>>     renameStubs(file)
    > >>>   File "C:\JPDump\test.py", line 15, in renameStubs
    > >>>     os.chdir (path)
    > >>> WindowsError: [Error 123] The filename, directory name, or volume
    > >>> label syntax is incorrect: '\\?\\C:\\TestDataSet\
    > >>> \Many_Files_10000_1KB_FIles\\001_0009_1000 FILES\\'
    > >>> The value of my path variable is
    > >>> \?\C:\TestDataSet\Many_Files_10000_1KB_FIles\001_0009_1000 FILES\
    > >> There need to be two backslashes at the beginning:

    >
    > >> \\?\C:\TEST.....FILES\

    >
    > >> Note the double backslash before the question mark.

    >
    > >> TJG

    >
    > > I've another situation where os.chdir() function failed. Please find
    > > the traceback pasted below:
    > > 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: '\\?\
    > > \C:\\TestDataSet\\DeepPaths\\DeepPathLevel01\\DeepPathLevel02\
    > > \DeepPathLevel03\\DeepPathLevel04\\DeepPathLevel05\\DeepPathLevel06\
    > > \DeepPathLevel07\\DeepPathLevel08\\DeepPathLevel09\\DeepPathLevel10\
    > > \DeepPathLevel11\\DeepPathLevel12\\DeepPathLevel13\\DeepPathLevel14\
    > > \DeepPathLevel15\\DeepPathLevel16\\'

    >
    > Try it as a unicode string:
    >
    > os.chdir (ur"\\?\c:\test...\deep...")
    >
    > TJG


    Sorry.. even that failed:

    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\\'
    ---------------------------------------------------------------------------------------------------------------
    Here is my code snippet which you will be interested in:

    file = ur'\\?\C:\\TestDataSet\DeepPaths
    \DeepPathLevel01\DeepPathLevel02\DeepPathLevel03\DeepPathLevel04\DeepPathLevel05\DeepPathLevel06\DeepPathLevel07\DeepPathLevel08\DeepPathLevel09\DeepPathLevel10\DeepPathLevel11\DeepPathLevel12\DeepPathLevel13\DeepPathLevel14\DeepPathLevel15\DeepPathLevel16\DeepPathLevel172.txt'
    def renameStubs(file):
    #e.write(u"\n"+strftime("%Y-%m-%d %H:%M:%S") +" we are in
    renameStubs function \n")
    drive = file.split(":")[0]
    oldName = file.split("\\")[-1]
    path = file.rstrip(oldName)
    os.chdir (path)
    ............
    renameStubs(file)
    , Mar 11, 2009
    #5
  6. Tim Golden Guest

    > Here is my code snippet which you will be interested in:

    Indeed.

    > file = ur'\\?\C:\\TestDataSet\DeepPaths
    > \DeepPathLevel01\DeepPathLevel02\DeepPathLevel03\DeepPathLevel04\DeepPathLevel05\DeepPathLevel06\DeepPathLevel07\DeepPathLevel08\DeepPathLevel09\DeepPathLevel10\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
    Tim Golden, Mar 11, 2009
    #6
  7. Guest

    On Mar 11, 5:19 pm, Tim Golden <> 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\DeepPathLevel05\DeepPathLevel06\DeepPathLevel07\DeepPathLevel08\DeepPathLevel09\DeepPathLevel10\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\\'

    Please help..
    Thank you,
    Venu M
    , Mar 11, 2009
    #7
  8. Tim Golden Guest

    wrote:
    > On Mar 11, 5:19 pm, Tim Golden <> 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\DeepPathLevel05\DeepPathLevel06\DeepPathLevel07\DeepPathLevel08\DeepPathLevel09\DeepPathLevel10\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
    Tim Golden, Mar 11, 2009
    #8
  9. Guest

    On Mar 11, 6:41 pm, Tim Golden <> wrote:
    > wrote:
    > > On Mar 11, 5:19 pm, Tim Golden <> 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.
    , Mar 11, 2009
    #9
  10. Tim Golden Guest

    wrote:
    > On Mar 11, 6:41 pm, Tim Golden <> wrote:
    >> wrote:
    >>> On Mar 11, 5:19 pm, Tim Golden <> 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
    Tim Golden, Mar 11, 2009
    #10
  11. Guest

    On Mar 11, 7:17 pm, Tim Golden <> wrote:
    > wrote:
    > > On Mar 11, 6:41 pm, Tim Golden <> wrote:
    > >> wrote:
    > >>> On Mar 11, 5:19 pm, Tim Golden <> 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


    Sir,
    My application has to rename a file in that folder.For that I had
    to do a os.chdir() to that folder. Otherwise if I do a os.rename
    (deeppath\file1,file2), it is creating a new file in the current
    working directory with the new name and leaving the original file as
    it is which is not intended :-(. So, can you suggest me any work
    around for this?

    Thank you
    Venu.
    , Mar 11, 2009
    #11
  12. Tim Golden Guest

    wrote:
    > On Mar 11, 7:17 pm, Tim Golden <> wrote:
    >> wrote:
    >>> On Mar 11, 6:41 pm, Tim Golden <> wrote:
    >>>> wrote:
    >>>>> On Mar 11, 5:19 pm, Tim Golden <> 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

    >
    > Sir,
    > My application has to rename a file in that folder.For that I had
    > to do a os.chdir() to that folder. Otherwise if I do a os.rename
    > (deeppath\file1,file2), it is creating a new file in the current
    > working directory with the new name and leaving the original file as
    > it is which is not intended :-(. So, can you suggest me any work
    > around for this?


    Jus
    t rename from and to with a full pathname (using the \\?\ bit):

    os.rename (ur"\\?\c:\long\path\to\file.txt", ur"\\?\c:\long\path\to\newfile.txt")

    TJG
    Tim Golden, Mar 11, 2009
    #12
  13. En Wed, 11 Mar 2009 11:59:57 -0200,
    <> escribió:
    > On Mar 11, 6:41 pm, Tim Golden <> wrote:
    >> wrote:
    >> > On Mar 11, 5:19 pm, Tim Golden <> 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
    Gabriel Genellina, Mar 11, 2009
    #13
  14. "Tim Golden" <ma...lden.me.uk> wrote:

    > 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


    Is this also true if you try to go there by a succession
    of shorter hops of the ./next_level kind?

    - Hendrik
    Hendrik van Rooyen, Mar 11, 2009
    #14
  15. Tim Golden Guest

    Hendrik van Rooyen wrote:
    > "Tim Golden" <ma...lden.me.uk> wrote:
    >
    >> 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

    >
    > Is this also true if you try to go there by a succession
    > of shorter hops of the ./next_level kind?
    >
    > - Hendrik



    Yep. Seems to be. You reach a certain point and presumably
    the internal "this is where I am" buffer runs out of oomph.

    TJG
    Tim Golden, Mar 11, 2009
    #15
  16. van Asselt Guest

    Hello,

    In order to prevent this type of problems, I alway do the following:

    import path

    path = something
    path = os.path.normpath(path)
    os.chdir(path)

    This prevents a lot of problems for me.

    Regards,
    Henk

    "Tim Golden" <> wrote in message
    news:...
    > wrote:
    >> Hello all,
    >> I am writing a python script which has to access deep paths
    >> then supported normally by the Windows OS (>255). So I am appending "\
    >> \?\" to do so. But when I use the path in the above fashion with
    >> os.chdir() it is unable to recognize my folder and throwing an error:
    >>
    >> Traceback (most recent call last):
    >> File "C:\JPDump\test.py", line 31, in <module>
    >> renameStubs(file)
    >> File "C:\JPDump\test.py", line 15, in renameStubs
    >> os.chdir (path)
    >> WindowsError: [Error 123] The filename, directory name, or volume
    >> label syntax is incorrect: '\\?\\C:\\TestDataSet\
    >> \Many_Files_10000_1KB_FIles\\001_0009_1000 FILES\\'
    >>
    >> The value of my path variable is
    >> \?\C:\TestDataSet\Many_Files_10000_1KB_FIles\001_0009_1000 FILES\

    >
    >
    > There need to be two backslashes at the beginning:
    >
    > \\?\C:\TEST.....FILES\
    >
    > Note the double backslash before the question mark.
    >
    > TJG
    van Asselt, Mar 11, 2009
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Schull

    cgi-tb after os.chdir

    Jon Schull, Jul 14, 2003, in forum: Python
    Replies:
    0
    Views:
    406
    Jon Schull
    Jul 14, 2003
  2. Yun Mao

    os.environ and os.path.chdir

    Yun Mao, Sep 20, 2003, in forum: Python
    Replies:
    4
    Views:
    437
    Thomas Bellman
    Sep 20, 2003
  3. Tom McDavid
    Replies:
    0
    Views:
    476
    Tom McDavid
    May 13, 2004
  4. Philippe C. Martin

    os.chdir + GUI question

    Philippe C. Martin, Dec 3, 2005, in forum: Python
    Replies:
    0
    Views:
    298
    Philippe C. Martin
    Dec 3, 2005
  5. Replies:
    3
    Views:
    598
    DataSmash
    Jun 2, 2006
Loading...

Share This Page