Process multiple files

Robert Bossy Robert.Bossy at jouy.inra.fr
Mon Apr 14 14:48:47 CEST 2008


Doran, Harold wrote:
> Say I have multiple text files in a single directory, for illustration
> they are called "spam.txt" and "eggs.txt". All of these text files are
> organized in exactly the same way. I have written a program that parses
> each file one at a time. In other words, I need to run my program each
> time I want to process one of these files.
>
> However, because I have hundreds of these files I would like to be able
> to process them all in one fell swoop. The current program is something
> like this:
>
> sample.py
> new_file = open('filename.txt', 'w')
> params = open('eggs.txt', 'r')
> 	do all the python stuff here
> new_file.close()
>
> 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.
>   
Hi,

It seems that you need glob.glob() :
http://docs.python.org/lib/module-glob.html#l2h-2284

import glob
for txt_filename in 
glob.glob('/path/to/the/dir/containing/your/files/*.txt'):
    print txt_filename # or do your stuff with txt_filename

RB



More information about the Python-list mailing list