file names longer than MAX_PATH under Windows 2003

Discussion in 'Python' started by Sergey, Feb 14, 2006.

  1. Sergey

    Sergey Guest

    Hello.

    I try to open file with pathname length 282 bytes:
    E:\files\..................\something.dat

    On MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp) described method to access
    files with path length
    up to 32000 bytes: just add prefix \\?\ to file name.
    But when I try to pass prefixed name to file(), I get the same result as when I don't add the prefix: file not found. May be Python
    just doesn't support long unicode filenames?
    Is there way to open such files?
     
    Sergey, Feb 14, 2006
    #1
    1. Advertising

  2. On Tue, 14 Feb 2006 14:29:44 +0300, Sergey wrote:

    > Hello.
    >
    > I try to open file with pathname length 282 bytes:
    > E:\files\..................\something.dat
    >
    > On MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp) described method to access
    > files with path length
    > up to 32000 bytes: just add prefix \\?\ to file name.
    > But when I try to pass prefixed name to file(), I get the same result as when I don't add the prefix: file not found. May be Python
    > just doesn't support long unicode filenames?
    > Is there way to open such files?


    Backslashes have special meaning to Python and need to be escaped. If you
    do this:

    f = file("E:\files\...\something.dat", "r")

    Python's string escape rules means you are actually trying to open the
    file "E:files...something.dat" which doesn't exist.

    You should escape the backslashes:

    f = file("E:\\files\\...\\something.dat", "r")

    or use raw strings:

    f = file(r"E:\files\...\something.dat", "r")

    or just use forward slashes and let Windows deal with it:

    f = file("E:/files/.../something.dat", "r")


    Does this help?



    --
    Steven.
     
    Steven D'Aprano, Feb 14, 2006
    #2
    1. Advertising

  3. Sergey

    Sergey Guest

    "Steven D'Aprano" <> wrote in message news:p...
    > f = file("E:/files/.../something.dat", "r")
    > Does this help?


    backslashes quoted.

    >>> print c

    \\.\e:\files\xxxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx\xxxxxxxxxxx xxxxxxxxx
    \xxxxxxxxxxxxxxxxxxxxxxx\xxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxx\xxxxxxxxxxxx
    xxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxx\xxxxxx xxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xls

    (cyrillic letters in filename here I replaced with x-es)

    >>> file(c,"rb")

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    IOError: [Errno 2] No such file or directory: u'\\\\.\\e:\\files\\\u041f\u0420....filename skipped....\u0442.xls'

    >>> win32file.CreateFile(c,win32file.GENERIC_READ,0,None,win32file.OPEN_EXISTING

    ,0,None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    pywintypes.error: (206, 'CreateFile', 'The filename or extension is too long.')

    When I open explorer, I can browse folder and see this file (but I can do almost nothing with it). It was created through share.
    Ntbackup can work with the file - why Python can't?
     
    Sergey, Feb 14, 2006
    #3
  4. On Tue, 14 Feb 2006 22:43:50 +1100, Steven D'Aprano wrote:

    > On Tue, 14 Feb 2006 14:29:44 +0300, Sergey wrote:
    >
    >> Hello.
    >>
    >> I try to open file with pathname length 282 bytes:
    >> E:\files\..................\something.dat
    >>
    >> On MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp) described method to access
    >> files with path length
    >> up to 32000 bytes: just add prefix \\?\ to file name.
    >> But when I try to pass prefixed name to file(), I get the same result as when I don't add the prefix: file not found. May be Python
    >> just doesn't support long unicode filenames?
    >> Is there way to open such files?

    >
    > Backslashes have special meaning to Python and need to be escaped. If you
    > do this:
    >
    > f = file("E:\files\...\something.dat", "r")
    >
    > Python's string escape rules means you are actually trying to open the
    > file "E:files...something.dat" which doesn't exist.


    [slaps head]
    I seem to be a bit confused about string escaping rules. Only some
    backslashes have special meaning, the rest remain in the string.

    Sergey, you said UNICODE file names. Not just ordinary strings.

    Are you passing a unicode object to the function?

    f = file(u"E:\\files\\...\\something.dat", "r")


    --
    Steven.
     
    Steven D'Aprano, Feb 14, 2006
    #4
  5. Sergey

    Sergey Guest

    "Steven D'Aprano" <> wrote in message news:p...
    > Are you passing a unicode object to the function?
    >
    > f = file(u"E:\\files\\...\\something.dat", "r")


    I pass variable c into functions:

    >>> c

    u'\\\\.\\e:\\files\\\u041f\u0420\u041e\u0414\u041e \u041c\u0435\u043d\u0435\u043
    [many unicode chars skipped]
    44b\u0439_\u0411\u044e\u0434\u0436\u0435\u0442.xls'
     
    Sergey, Feb 14, 2006
    #5
    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. Sunner Sun
    Replies:
    23
    Views:
    9,453
    Dan Pop
    Apr 13, 2004
  2. Tim Golden
    Replies:
    2
    Views:
    326
    Sergey
    Feb 14, 2006
  3. Tim Golden
    Replies:
    3
    Views:
    998
    Sergey
    Feb 15, 2006
  4. Tim Golden
    Replies:
    2
    Views:
    348
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Feb 15, 2006
  5. sonet

    How to change MAX_PATH value?

    sonet, Nov 28, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    515
    Ilya Zakharevich
    Dec 4, 2007
Loading...

Share This Page