[Tutor] What is missing?
Sun, 03 Feb 2002 21:33:42 -0700
Ramkumar Kashyap wrote:
> inFile.write(filename + ', ' + string(count))
> There are 325 files in the directory. The program works up until the
> print statement which displays all the 325 files with the corresponding
> lines contained in the respective files. The write statement only writes
> the last file and line_numbers for the last file. Should I put it in
> another loop?
Heh, be careful when assuming things about your code. The write
statement will in fact write 325 times, its just you only SEE the last
one because each time through the loop the call to open() inFile
obliterates the old file.
You can either move the infile = open... line outside the loop (on top),
open inFile in 'a' (append) mode, or form a new output filename for each
file. My personal preference would be for the first option.
BTW two other comments:
1) Using 'inFile' for an output file is very confusing.
2) It's generally good practice to explicitly close files, even though
in Python you don't really have to. Explicit closing helps document the
code and helps ensure that what you want to happen is indeed what is
Hope that helps.
Paul Sidorsky Calgary, Canada