[Python-3000] Path Reform: Get the ball rolling
Georg Brandl
g.brandl at gmx.net
Thu Nov 2 17:13:46 CET 2006
Mike Orr wrote:
> The thread on python-dev has been revived, so those interested in this
> subject will want to look in both places.
>
> On 11/1/06, Talin <talin at acm.org> wrote:
>> Actually I generally use:
>>
>> p = os.path.normpath( os.path.join( __file__, "../..", "lib" ) )
>>
>> or even:
>>
>> p = os.path.normpath( os.path.join( __file__, "../../lib" ) )
>>
>> ...which isn't quite as concise as what you wrote, but is better than
>> the first example. (The reason this works is because 'normpath' doesn't
>> know whether the last component is a file or a directory -- it simply
>> interprets the ".." as an instruction to strip off the last component.)
>
> This illustrates two problems with os.path. The reason I use all
> these nested functions instead of a simple normpath/join is one is
> told "it's bad to use platform-specific separators". Perhaps this
> disrecommendation should be lifted, especially since both Mac and
> Windows do the right thing with "/", "..", and "." now.
>
> The other thing is, ".." and "." seem to be smarter than
> os.path.dirname. I can't quite articulate the rules but '.' off a
> file chops the filename component, while '.' off a directory does
> nothing. '..' off a file goes to the file's directory, while '..' off
> a directory goes to the directory's parent. Dirname() just chops the
> final component without asking what it is, while '..' and '.' do
> different things depending on whether the final component is a
> directory. I think a method like .ancestor(N) would be useful,
> meaning "do '..' N times.
>
> /a/b/../c # Previous component is always a directory, so eliminate.
"." and ".." on a file is meaningless on Unix:
$ ls /etc/passwd/../
/bin/ls: cannot access /etc/passwd/../: Not a directory
Georg
More information about the Python-3000
mailing list