On 3 Oct 2010, at 02:35, Nir Soffer wrote:

On Sat, Sep 25, 2010 at 1:36 AM, James Y Knight <f...@fuhm.net> wrote:

An OSX code sketch is available here (summary: call FSPathMakeRef to get an FSRef from a path string, then FSRefMakePath to make it back into a path, which will then have the correct case). And note that it only works if the
file actually exists.


http://stackoverflow.com/questions/370186/how-do-i-find-the-correct-case-of-a-filename

It would indeed be useful to have that be available in Python.


There is a much simpler way:

from Carbon import File
File.FSRef('/tmp/foo').as_pathname()
'/private/tmp/Foo'

Note that this is much slower compared to os.path.exists.

This won't work in py3k; the Carbon modules were removed in 3.0. A simpler alternative would probably be the F_GETPATH fcntl. An example:

Python 3.1.2 (r312:79147, Jul 11 2010, 18:21:56)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from fcntl import fcntl
>>> from os.path import basename, exists
>>> from os import remove
>>>
>>> F_GETPATH = 50
>>>
>>> if exists('/tmp/å'):
...   remove('/tmp/å')
...
>>> open('/tmp/å', 'w').close()
>>> f = open(b'/tmp/A\xcc\x8a')
>>>
>>> a = f.name
>>> b = fcntl(f, F_GETPATH, b'\0' * 1024).rstrip(b'\0')
>>>
>>> a, b
(b'/tmp/A\xcc\x8a', b'/private/tmp/\xc3\xa5')
>>> a.decode('utf-8'), b.decode('utf-8')
('/tmp/Å', '/private/tmp/å')

--

Dan Villiom Podlaski Christiansen
dan...@gmail.com

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to