fileinput module doesn't work to spec?

Alex Martelli aleaxit at yahoo.com
Tue Oct 24 12:16:28 EDT 2000


"Dale Strickland-Clark" <dale at out-think.NOSPAMco.uk> wrote in message
news:0s8bvs0n0e4d0t462mqlknha2ekj7grm22 at 4ax.com...
> According to the spec, the fileinput module should support overwriting
> of the input file after renaming it to preserve a backup. (Extract

If you want the backup kept, you have to provide a 3rd argument
to fileinput.input (the extension to use); else, it's removed
when its end-of-file is reached.

Which is the key to your bug, I think...:

for line in fileinput.input(sys.argv[1:], inplace = 1):
    table = table + genRow(line)

print ''.join(table)

You're writing to standard output *after* the input file[s]
are at EOF -- by that time, the input file[s] have already
been replaced with empty versions.


What happens if you change these lines to...:

for line in fileinput.input(sys.argv[1:], inplace = 1):
    newone = genRow(line)
    table = table + newone
    sys.stdout.write(newone)

i.e., ensure you're writing to stdout DURING the reading
process?  I think that, this way, it should work.  Maybe
the documentation for fileinput.input is unclear about
this, in which case I'm sure Fred Drake and the other
hard-working Python documenters would dearly appreciate a
bug-submission to sourceforge including suggested wording
for a replacement or addition...


Alex






More information about the Python-list mailing list