Process multiple files
Duncan Booth
duncan.booth at invalid.invalid
Mon Apr 14 09:15:48 EDT 2008
"Doran, Harold" <HDoran at air.org> wrote:
> If these files followed a naming convention such as 1.txt and 2.txt I
> can easily see how these could be parsed consecutively in a loop.
> However, they are not and so is it possible to modify this code such
> that I can tell python to parse all .txt files in a certain directory
> and then to save them as separate files? For instance, using the example
> above, python would parse both spam.txt and eggs.txt and then save 2
> different files, say as spam_parsed.txt and eggs_parsed.txt.
>
This isn't exactly what you asked for, but have you looked at the fileinput
module: it will process a list of filenames pointing stdin at each file in
turn, optionally it can work inplace, but without that option I think it is
pretty close to what you want (warning, untested code follows):
output = None
for line in fileinput.input(n for n in glob.glob('*.txt')
if not n.endswith('_parsed.txt')):
if fileinput.isfirstline():
if output: output.close()
output = open(fileinput.filename().replace('.', '_parsed.', 1),
'w')
print >>output, "parsed:", line.strip()
if output:
output.close()
More information about the Python-list
mailing list