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

Christian Heimes christian at python.org
Sun Oct 7 00:41:24 CEST 2012


Am 05.10.2012 20:25, schrieb Antoine Pitrou:
> Hello,
> 
> This PEP is a resurrection of the idea of having object-oriented
> filesystem paths in the stdlib. It comes with a general API proposal
> as well as a specific implementation (*). The implementation is young
> and discussion is quite open.

I already gave you my +1 on #python-dev. I've some additional ideas that
I like to suggest for pathlib.

* Jason Orendorff's path module has some methods that are quite useful
for shell and find like script. I especially like the
files(pattern=None), dirs(pattern=None) and their recursive counterparts
walkfiles() and walkdirs(). They make code like recursively remove all
pyc files easy to write:

  for pyc in path.walkfiles('*.py'):
      pyc.remove()

* I like to see a convenient method to format sizes in SI units (for
example 1.2 MB, 5 GB) and non SI units (MiB, GiB, aka human readable,
multiple of 2). I've some code that would be useful for the task.

* Web application often need to know the mimetype of a file. How about a
mimetype property that returns the mimetype according to the extension?

* Symlink and directory traversal attacks are a constant thread. I like
to see a pathlib object that restricts itself an all its offsprings to a
directory. Perhaps this can be implemented as a proxy object around a
pathlib object?

* While we are working on pathlib I like to improve os.listdir() in two
ways. The os.listdir() function currently returns a list of file names.
This can consume lots of memory for a directory with hundreds of
thousands files. How about I implement an iterator version that returns
some additional information, too? On Linux and most BSD you can get the
file type (d_type, e.g. file, directory, symlink) for free.

* Implement "if filename in directory" with os.path.exists().

Christian



More information about the Python-ideas mailing list