Windows/win32all, unicode and long filenames

Discussion in 'Python' started by Neil Hodgson, Aug 28, 2005.

  1. Neil Hodgson

    Neil Hodgson Guest

    Kevin Ollivier:

    > On Windows, it's very common to have a string of long directories in the
    > pathname for files, like "C:\Documents and Settings\My Long User Name\My
    > Documents\My Long Subdirectory Name\...". For a wxPython application I'm
    > working on, this has actually caused me to run into what appears to be
    > Python's pathname length limit for opening files. (247 chars on Win) Yes,
    > I can hear people saying "yipes!" but this stuff does happen sometimes on
    > Windows. :)


    The Win32 API restricts path arguments to 260 characters. Longer
    paths can be passed to wide character functions by using a "\\?" prefix.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp
    This may work from Python although I haven't tested it.

    > My first inclination was to use win32api.GetShortPathName(mypath),
    > ...
    > Looking at the pyWin32 sources, it does look like only the ASCII version
    > of this function exists, which suggests that for now this route is a
    > dead-end.


    All APIs can be accessed through ctypes.

    Neil
    Neil Hodgson, Aug 28, 2005
    #1
    1. Advertising

  2. Hi all,

    On Windows, it's very common to have a string of long directories in the
    pathname for files, like "C:\Documents and Settings\My Long User Name\My
    Documents\My Long Subdirectory Name\...". For a wxPython application I'm
    working on, this has actually caused me to run into what appears to be
    Python's pathname length limit for opening files. (247 chars on Win) Yes,
    I can hear people saying "yipes!" but this stuff does happen sometimes on
    Windows. :)

    My first inclination was to use win32api.GetShortPathName(mypath), which
    worked fine until I had an unicode pathname with non-ascii characters in
    it. Those give me the 'oridnal not in
    range' errors performing an ascii encode, meaning that GetShortPathName
    doesn't handle unicode objects. The problem is that I'm working with
    Unicode filenames that contain characters that are not only non-ascii
    characters, but characters not in the current locale's character set as
    well. So I can't just 'down convert' from a unicode object to a string
    object in the current character set without corrupting the filename.
    Looking at the pyWin32 sources, it does look like only the ASCII version
    of this function exists, which suggests that for now this route is a
    dead-end.

    The only other solution I could think of is to call
    os.chdir(long_pathname) and open the file using a filename relative to
    long_pathname instead of an absolute path. But I was wondering if there
    was another solution, preferably one that doesn't require me to muck with
    the current directory, and I was also wondering if there was a simple way
    to get one or both of the above limitations removed. :)

    Thanks in advance for any help,

    Kevin
    Kevin Ollivier, Aug 28, 2005
    #2
    1. Advertising

  3. On Sat, 27 Aug 2005 23:30:46 +0000, Neil Hodgson wrote:

    > Kevin Ollivier:
    >
    >> On Windows, it's very common to have a string of long directories in the
    >> pathname for files, like "C:\Documents and Settings\My Long User Name\My
    >> Documents\My Long Subdirectory Name\...". For a wxPython application I'm
    >> working on, this has actually caused me to run into what appears to be
    >> Python's pathname length limit for opening files. (247 chars on Win) Yes,
    >> I can hear people saying "yipes!" but this stuff does happen sometimes on
    >> Windows. :)

    >
    > The Win32 API restricts path arguments to 260 characters. Longer
    > paths can be passed to wide character functions by using a "\\?" prefix.
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp
    > This may work from Python although I haven't tested it.
    >
    >> My first inclination was to use win32api.GetShortPathName(mypath),
    >> ...
    >> Looking at the pyWin32 sources, it does look like only the ASCII version
    >> of this function exists, which suggests that for now this route is a
    >> dead-end.

    >
    > All APIs can be accessed through ctypes.


    Thanks for the tips! I had heard of ctypes but it never crossed my mind it
    was for accessing functions from shared libraries. :) I'll try both of
    your tips out, and one of them should resolve the problem.

    Kevin

    > Neil
    Kevin Ollivier, Aug 28, 2005
    #3
  4. Hi !


    You are true.
    But, more, don't believe : for use with CD-Rom/DVD, a path cannot to have
    more than 64 caracteres.


    @-salutations

    Michel Claveau
    Do Re Mi chel La Si Do, Aug 28, 2005
    #4
    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. DrkShadow

    Windows and long filenames

    DrkShadow, Aug 19, 2004, in forum: Perl
    Replies:
    2
    Views:
    463
  2. B.J.
    Replies:
    4
    Views:
    725
    Toby Inkster
    Apr 23, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    458
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    778
    Peter Nilsson
    Jan 15, 2008
  5. Jian Lin
    Replies:
    2
    Views:
    180
    Jian Lin
    Jun 27, 2010
Loading...

Share This Page