[Q] In-place file update with fileinput module
Arsenal
arsenal_hl at yahoo.com.hk
Sat Jun 21 06:27:00 EDT 2003
Hi,
I want to do some in-place editing with a text file. Initially i
didn't know the name that reference the file object "abc.txt". So i
did some testing and found that sys.stdout was redirected to
"abc.txt".
and hence the following piece of test code:
for line in fileinput.input(r'abc.txt', inplace=1):
if line.find('replaceme') >= 0:
#replace 'replaceme' with 'done'
sys.stdout.write('done')
else:
#write the line unchanged
sys.stdout.write(line)
Is this the right (or best) way to do it with "sys.stdout.write"?
Thanks!
More information about the Python-list
mailing list