os.path.getmtime on windows, error: 206 - path or extension too long

Discussion in 'Python' started by mynthon, Jan 21, 2009.

  1. mynthon

    mynthon Guest

    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

    # code
    def getmtimeWIN32(p):
    mycwd = os.getcwd()

    if p.startswith('\\\\?\\'):
    p = p.replace('\\\\?\\', '', 1)

    p = os.path.splitdrive(p)
    r = p[0] # root - dir name
    p = p[1]
    p = os.path.split(p)
    f = p[1] # filename
    d = p[0]
    l = d.split('\\');

    if r != '': # if root is not empty change to root (it not works
    when script is on other partition than file)
    os.chdir('/')

    for i in l:
    if i != '':
    os.chdir(i)
    #print i
    print os.getcwd()
    os.path.getmtime(f)
    os.chdir(mycwd)
    # /code

    it works for other files so i suppose it is not my fault. 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).

    (python 2.5)
     
    mynthon, Jan 21, 2009
    #1
    1. Advertising

  2. mynthon

    mynthon Guest

    Re: os.path.getmtime on windows, error: 206 - path or extension toolong

    On Jan 21, 11:50 am, mynthon <> wrote:
    > 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
    >
    > # code
    > def getmtimeWIN32(p):
    >     mycwd = os.getcwd()
    >
    >     if p.startswith('\\\\?\\'):
    >         p = p.replace('\\\\?\\', '', 1)
    >
    >     p = os.path.splitdrive(p)
    >     r = p[0] # root - dir name
    >     p = p[1]
    >     p = os.path.split(p)
    >     f = p[1] # filename
    >     d = p[0]
    >     l = d.split('\\');
    >
    >     if r != '': # if root is not empty change to root (it not works
    > when script is on other partition than file)
    >         os.chdir('/')
    >
    >     for i in l:
    >         if i != '':
    >             os.chdir(i)
    >             #print i
    >     print os.getcwd()
    >     os.path.getmtime(f)
    >     os.chdir(mycwd)
    > # /code
    >
    > it works for other files so i suppose it is not my fault. 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).
    >
    > (python 2.5)



    ok, what ive found:

    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)
     
    mynthon, Jan 21, 2009
    #2
    1. Advertising

  3. mynthon

    mynthon Guest

    Re: os.path.getmtime on windows, error: 206 - path or extension toolong

    On Jan 21, 2:13 pm, mynthon <> wrote:
    > On Jan 21, 11:50 am, mynthon <> wrote:
    >
    >
    >
    > > 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

    >
    > > # code
    > > def getmtimeWIN32(p):
    > >     mycwd = os.getcwd()

    >
    > >     if p.startswith('\\\\?\\'):
    > >         p = p.replace('\\\\?\\', '', 1)

    >
    > >     p = os.path.splitdrive(p)
    > >     r = p[0] # root - dir name
    > >     p = p[1]
    > >     p = os.path.split(p)
    > >     f = p[1] # filename
    > >     d = p[0]
    > >     l = d.split('\\');

    >
    > >     if r != '': # if root is not empty change to root (it not works
    > > when script is on other partition than file)
    > >         os.chdir('/')

    >
    > >     for i in l:
    > >         if i != '':
    > >             os.chdir(i)
    > >             #print i
    > >     print os.getcwd()
    > >     os.path.getmtime(f)
    > >     os.chdir(mycwd)
    > > # /code

    >
    > > it works for other files so i suppose it is not my fault. 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).

    >
    > > (python 2.5)

    >
    > ok, what ive found:
    >
    > 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)


    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:\\'
     
    mynthon, Jan 21, 2009
    #3
  4. mynthon

    mynthon Guest

    Re: os.path.getmtime on windows, error: 206 - path or extension toolong

    On Jan 21, 2:58 pm, mynthon <> wrote:
    > On Jan 21, 2:13 pm, mynthon <> wrote:
    >
    >
    >
    > > On Jan 21, 11:50 am, mynthon <> wrote:

    >
    > > > 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

    >
    > > > # code
    > > > def getmtimeWIN32(p):
    > > >     mycwd = os.getcwd()

    >
    > > >     if p.startswith('\\\\?\\'):
    > > >         p = p.replace('\\\\?\\', '', 1)

    >
    > > >     p = os.path.splitdrive(p)
    > > >     r = p[0] # root - dir name
    > > >     p = p[1]
    > > >     p = os.path.split(p)
    > > >     f = p[1] # filename
    > > >     d = p[0]
    > > >     l = d.split('\\');

    >
    > > >     if r != '': # if root is not empty change to root (it not works
    > > > when script is on other partition than file)
    > > >         os.chdir('/')

    >
    > > >     for i in l:
    > > >         if i != '':
    > > >             os.chdir(i)
    > > >             #print i
    > > >     print os.getcwd()
    > > >     os.path.getmtime(f)
    > > >     os.chdir(mycwd)
    > > > # /code

    >
    > > > it works for other files so i suppose it is not my fault. 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).

    >
    > > > (python 2.5)

    >
    > > ok, what ive found:

    >
    > > 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)

    >
    > 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:\\'


    you can also use \\?\ prefix but ONLY FOR unicode strings


    os.path.getmtime("\\\\?\\c:\\very lon path\\blablabla") #will not work
    os.path.getmtime(u"\\\\?\\c:\\very lon path\\blablabla") #will work
     
    mynthon, Jan 21, 2009
    #4
  5. Re: os.path.getmtime on windows, error: 206 - path or extension toolong

    En Wed, 21 Jan 2009 08:50:53 -0200, mynthon <> escribió:

    > 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

    [...]
    > it works for other files so i suppose it is not my fault. 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?


    win32api is part of the pywin32 package, available at http://pywin32.sf.net
    This is not a Python standard package - you either installed it yourself
    some time ago (and forgot about it) or perhaps you're using the Enthought
    bundle (which includes pywin32 and PythonWin). Either way, you surely have
    the PyWin32.chm help file installed too.

    En Wed, 21 Jan 2009 12:11:03 -0200, mynthon <> escribió:

    > you can also use \\?\ prefix but ONLY FOR unicode strings
    >
    > os.path.getmtime("\\\\?\\c:\\very lon path\\blablabla") #will not work
    > os.path.getmtime(u"\\\\?\\c:\\very lon path\\blablabla") #will work


    Looks like you found the "official" answer to your problem. Notice that
    the path must be "normalized" in this case - that means, it must not
    contain "." nor ".." components.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 22, 2009
    #5
  6. mynthon

    krowey

    Joined:
    Feb 9, 2013
    Messages:
    1
    path too long

    When you encounter problems like this it's better to use a third party app than to use the options you mentioned. Try longpathtool. It can solve issues like this and other windows error like error 1320.
     
    krowey, Feb 9, 2013
    #6
    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. =?ISO-8859-1?Q?Jorg_R=F8dsj=F8?=

    os.path.getmtime on winXP

    =?ISO-8859-1?Q?Jorg_R=F8dsj=F8?=, Nov 8, 2005, in forum: Python
    Replies:
    5
    Views:
    4,611
    Bengt Richter
    Nov 8, 2005
  2. robert
    Replies:
    1
    Views:
    4,302
    Dennis Lee Bieber
    Jul 1, 2006
  3. Replies:
    3
    Views:
    512
    Anthony Greene
    Sep 20, 2007
  4. Replies:
    0
    Views:
    597
  5. Tim Johnson
    Replies:
    0
    Views:
    262
    Tim Johnson
    Sep 14, 2011
Loading...

Share This Page