[Tutor] List's name in a string
"Héctor Villafuerte D."
hec.villafuerte at telgua.com.gt
Tue Sep 30 16:39:03 EDT 2003
Jeff Shannon wrote:
> 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?
>
Ok, you're right.... I should have said that I have multiple LISTs and
that I want to do something like this:
>>> file_w = open(LIST.name() + '_proc', 'w')
at least that's the basic idea.
I could write one open statement for every list in my program (there
aren't that many), but I'm trying to make a function
in order to generalize this my code. This function looks like this:
NOTES:
* traf is the expected list with filenames to process
def build_basic(traf):
"""Creates basic processed files."""
if traf:
file_w = open(traf.name() + '_proc', 'w') # THE PROBLEM
IS HERE (this is pseudocode)
for arch in traf:
for line in fileinput.input(arch):
file_w.write( line[0:8] + line[8:16] + line[16:22] +
line[22:36]
+ line[50:56] + line[62:69] + line[69:76]
+ '\n')
Thanks again.
More information about the Tutor
mailing list