[Python-Dev] Adding the 'path' module (was Re: Some RFE for review)

Phillip J. Eby pje at telecommunity.com
Sun Jun 26 20:17:15 CEST 2005


At 06:57 PM 6/26/2005 +0200, Reinhold Birkenfeld wrote:
>1226256:
>The "path" module by Jason Orendorff should be in the standard library.
>http://www.jorendorff.com/articles/python/path/
>Review: the module is great and seems to have a large user base. On c.l.py
>there are frequent praises about it.

I would note that there are some things in the interface that should be 
cleaned up before it becomes a stdlib module.  It has many ways to do the 
same thing, and many of its property and method names are confusing because 
they either do the same thing as a standard function, but have a different 
name (like the 'parent' property that is os.path.dirname in disguise), or 
they have the same name as a standard function but do something different 
(like the 'listdir()' method that returns full paths rather than just 
filenames).  I'm also not keen on the fact that it makes certain things 
properties whose value can change over time; i.e. ctime/mtime/atime and 
size really shouldn't be properties, but rather methods.  I'm also not sure 
how I feel about all the read/write methods that hide the use of file 
objects; these seem like they should remain file object methods, especially 
since PEP 343 will allow easy closing with something like:

     with closing(some_path.open('w')) as f:
         f.write(data)

Granted, this is more verbose than:

     some_path.write_bytes(data)

But brevity isn't always everything.  If these methods are kept I would 
suggest using different names, like "set_bytes()", "set_text()", and 
"set_lines()", because "write" sounds like something you do on an ongoing 
basis to a stream, while these methods just replace the file's entire contents.

Aside from all these concerns, I'm +1 on adding the module.

Here's my list of suggested changes:

* path.joinpath(*args)   -> path.subpath(*args)
* path.listdir()         -> path.subpaths()
* path.splitall()        -> path.parts()
* path.parent            -> path.dirname (and drop dirname() method)
* path.name              -> path.filename (and drop basename() method)
* path.namebase          -> path.filebase (maybe something more descriptive?)
* path.atime/mtime/ctime -> path.atime(), path.mtime(), path.ctime()
* path.size              -> path.filesize()

* drop getcwd(); it makes no sense on a path instance

* add a samepath() method that compares the absolute, case and 
path-normalized versions of two paths, and a samerealpath() method that 
does the same but with symlinks resolved.

And, assuming these file-content methods are kept:

* path.bytes()         -> path.get_file_bytes()
* path.write_bytes()   -> path.set_file_bytes() and path.append_file_bytes()
* path.text()          -> path.get_file_text()
* path.write_text()    -> path.set_file_text() and path.append_file_text()
* path.lines()         -> path.get_file_lines()
* path.write_lines()   -> path.set_file_lines() and path.append_file_lines()



More information about the Python-Dev mailing list