BMI Students

Thursday, April 20, 2006 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.

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

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


Post a Comment

<< Home