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

Ethan Furman ethan at stoneleaf.us
Sat Oct 6 22:19:54 CEST 2012


I was hesitant to put mine on PyPI because there's already a slew of 
others, but for the sake of discussion here it is [1].

Mine is str based, has no actual I/O components, and can easily be used 
in normal os, shutil, etc., calls.

Example usage:

job = '12345'
home = Path('c:/orders'/job)
work = Path('c:/work/')
for pdf in glob(work/'*.pdf'):
     dash = pdf.filename.index('-')
     dest = home/'reports'/job + pdf.filename[dash:]
     shutil.copy(pdf, dest)


Assuming I haven't typo'ed anything, the above code takes all the pdf 
files, removes the standard (and useless to me) header info before the 
'-' in the filename, then copies it over to its final resting place.

If I understand Antoine's Path, the code would look something like:

job = '12345'
home = Path('c:/orders/')[job]
work = Path('c:/work/')
for child in work:
     if child.ext != '.pdf':
         continue
     name = child.filename
     dash = name.index('-')
     dest = home['reports'][name]
     shutil.copy(str(child), str(dest))


My biggest objections are the extra str calls, and indexing just doesn't 
look like path concatenation.

~Ethan~

[1]http://pypi.python.org/pypi/strpath

P.S.
Oh, very nice ascii-art!



More information about the Python-ideas mailing list