[Pythonmac-SIG] cross platform os.path.nornpath problem
bill fancher
bfancher@mac.com
Thu, 19 Sep 2002 10:13:56 -0700
On Wednesday, September 18, 2002, at 11:53 AM, Magladry, Stephen wrote:
> we are working on a cross platform game and I am running into a
> "problem"
> with os.path.normpath. We have decided to use Unix style relative paths
> within our Python code. The system, written in c++, is then
> responsible for
> the conversion to a Mac specific path for file i/o. The problem is lies
> within some Python code. Here is a quick example.
>
>
> import os
> print os.path.normpath( "foo/goo/file.txt" )# a unix relative path
> (result) >>> :foo/goo/file.txt
>
> Well, of course! "/" are valid chars in a Mac file name, so the
> operation
> worked as intended. I would have liked to have the result be
> "file.txt".
I don't see why normpath would return that even with POSIX paths. (It
doesn't with the "unix python" installed in 10.2.)
> there a way within Python 2.2.1 that I could do this with writing our
> own
> special normalizer?
You should compose your paths with os.path.join:
>>> import os
>>> p = os.path.join('foo','goo','file.txt')
>>> p
':foo:goo:file.txt'
>>> os.path.normpath(p)
':foo:goo:file.txt'
>>>
and decompose them with os.path.split. E.g. to get "file.txt":
os.path.split('foo:goo:file.txt')[-1]
HTH,
--
bill
>
>
> Thanks,
>
>
> Stephen Magladry
>
> _______________________________________________
> Pythonmac-SIG maillist - Pythonmac-SIG@python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
>