[Python-ideas] PEP 428 - object-oriented filesystem paths

Stephen J. Turnbull turnbull at sk.tsukuba.ac.jp
Sat Oct 6 10:00:31 CEST 2012


Antoine Pitrou writes:
 > On Sat, 06 Oct 2012 01:27:49 +0100
 > Richard Oudkerk <shibturn at gmail.com>
 > wrote:
 > 
 > > On 06/10/2012 12:48am, Antoine Pitrou wrote:
 > > >>>> p = PureNTPath('c:/orders/12345/abc67890.dbf')
 > > >>>> >>>p.parent()[p.name.split('.')[0] + '.csv']
 > > > PureNTPath('c:\\orders\\12345\\abc67890.csv')
 > > >
 > > > Any suggestion to ease this use case a bit?
 > > 
 > > Maybe p.basename could be shorthand for p.name.split('.')[0].
 > 
 > Wouldn't there be some confusion with os.path.basename:
 > 
 > >>> os.path.basename('a/b/c.ext')
 > 'c.ext'

Not to mention standard Unix usage.  GNU basename will allow you to
specify a *particular* extension explicitly, which will be stripped if
present and otherwise ignored.  Eg, "basename a/b/c.ext ext" => "c."
(note the period!) and "basename a/b/c ext" => "c".  I don't know if
that's an extension to POSIX.  In any case, it would require basename
to be a method rather than a property.

 > (sorry for the earlier, unfinished reply)

Also there are applications where "basenames" contain periods (eg,
wget often creates directories with names like "www.python.org"), and
filenames may have multiple extensions, eg, "index.ja.html".

I think it's reasonable to define "extension" to mean "the portion
after the last period (if any, maybe including the period), but I
think usage of the complementary concept is pretty application-
specific.



More information about the Python-ideas mailing list