python noob, multiple file i/o

Jon Clements joncle at
Fri Mar 16 10:02:06 CET 2007

On 16 Mar, 03:56, "hiro" <Nun... at> wrote:
> Hi there,
> I'm very new to python, the problem I need to solve is whats the "best/
> simplest/cleanest" way to read in multiple files (ascii), do stuff to
> them, and write them out(ascii).
> --
> import os
> filePath = ('O:/spam/eggs/')
> for file in os.listdir(filePath):   #straight from docs
>     # iterate the function through all the files in the directory
>     # write results to separate files  <- this is where I'm mostly
> stuck.
> --
> For clarity's sake, the file naming conventions for the files I'm
> reading from are file.1.txt -> file.nth.txt
> It's been a long day, i'm at my wits end, so I apologize in advance if
> I'm not making much sense here.
> syntax would also be great if you can share some recipes.

I'd try the glob module.

import glob

# Get a list of filenames matching wildcard criteria
# (note that path is relative to working directory of program)
matching_file_list = glob.glob('O:/spam/eggs/*.txt')

# For each file that matches, open it and process it in some way...
for filename in matching_file_list:
    infile = file(filename)
    outfile = file(filename + '.out','w')
    # Process the input file line by line...
    for line in infile:
        pass # Do something more useful here, change line and write to
    # Be explicit with file closures

Of course, you can change the wild card criteria in the glob
statement, and also then filter further using regular expressions to
choose only files matching more specific criteria. This should be
enough to get you started though.



More information about the Python-list mailing list