BMI Students

Thursday, April 20, 2006

path.py: Useful python module for manipulating files.

Check out this path manipulation module. It looks quite handy, as it bundles a bunch of path-related things from the python standard library into a convenient class.
Here's a very simple example.

(Old)
import os
path = '/foo/bar/baz'
files = [f for f in os.listdir(path) if f.endswith('.txt')]
fullpath = os.path.join(path, 'hello.py')
f = open(fullpath, 'r')
lines = f.readlines()


(With path.py)
from path import path
path = path('/foo/bar/baz')
files = path.files('*.txt')
fullpath = path / 'hello.py'
lines = fullpath.lines()

0 Comments:

Post a Comment

<< Home