[Python-Dev] os.path.normcase rationale?
Dan Villiom Podlaski Christiansen
danchr at gmail.com
Sun Oct 3 15:18:39 CEST 2010
On 3 Oct 2010, at 02:35, Nir Soffer wrote:
> On Sat, Sep 25, 2010 at 1:36 AM, James Y Knight <foom at 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
danchr at gmail.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1943 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20101003/65a137b8/attachment.bin>
More information about the Python-Dev
mailing list