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

Michael Hoffman hoffman at ebi.ac.uk
Mon Jun 27 21:24:13 CEST 2005


On Mon, 27 Jun 2005, Phillip J. Eby wrote:

> At 08:20 AM 6/27/2005 +0100, Michael Hoffman wrote:
>> os.getcwd() returns a string, but path.getcwd() returns a new path
>> object.
>
> In that case, I'd expect it to be 'path.fromcwd()' or 'path.cwd()'; i.e. a
> constructor classmethod by analogy with 'dict.fromkeys()' or
> 'datetime.now()'.  'getcwd()' looks like it's getting a property of a path
> instance, and doesn't match stdlib conventions for constructors.
>
> So, +1 as long as it's called cwd() or something better (i.e. clearer
> and/or more consistent with stdlib constructor conventions).

+1 on cwd().

-1 on making this the default constructor. Essentially the default
constructor returns a path object that will reflect the CWD at the
time that further instance methods are called.

path.cwd() will return a path object that reflects the path at the
time of construction.

This example may be instructive:

>>> from path import path
>>> import os
>>>
>>> default_path = path()
>>> getcwd_path = path.getcwd()
>>> default_path.abspath()
path('/home/hoffman')
>>> getcwd_path.abspath()
path('/home/hoffman')
>>>
>>> os.chdir("etc")
>>> default_path.abspath()
path('/home/hoffman/etc')
>>> getcwd_path.abspath()
path('/home/hoffman')

Unfortunately only some of the methods work on paths created with the
default constructor:

>>> path().listdir()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/usr/lib/python2.4/site-packages/path.py", line 297, in listdir
     names = os.listdir(self)
OSError: [Errno 2] No such file or directory: ''

Is there support to have all of the methods work when the path is the
empty string? Among other benefits, this would mean that sys.path
could be turned into useful path objects with a simple list
comprehension.
-- 
Michael Hoffman <hoffman at ebi.ac.uk>
European Bioinformatics Institute



More information about the Python-Dev mailing list