[Tutor] List's name in a string

Jeff Shannon jeff at ccvcorp.com
Tue Sep 30 13:22:26 EDT 2003


Héctor Villafuerte D. wrote:

> You're right, I need to briefly explain what I want to do:
> * There's a list (LIST) which contains filenames (as strings); i.e.: 
> LIST = ['file1', 'file2'].
> * There's some processing on each file refered to by LIST.
> * The processed output should be stored in a file named 'LIST_proc' 
> (producing just one
> output file, appending the results from 'file1', 'file2', etc.)

Okay, that seems pretty straightforward.  I'd do this following a 
rough skeleton something like this:

outfile = file('LIST_proc', 'w')
for filename in LIST:
     infile = file(filename,'r')
     process_file(infile, outfile)
     infile.close()
outfile.close()

where process_file() reads lines from the input file, massages them in 
whatever way necessary, and writes the result to outfile.

To be honest, I'm not sure where the desire to get a variable by name 
came in; perhaps you were thinking that using names from the list was 
a little more complex than it really is?

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list