[Tutor] how to write a string into a specific line in a file

tak tak at dfx.co.jp
Tue Mar 7 11:22:09 CET 2006


Hello,

I was wondering if it is possible to write a string to a specific line
in a file
without reading in the whole file in as the below.
___________________________________________________________________

f = open(filename)
lines = f.readlines()
f.close()
# num for some line number
line[num] = "String"
f = open(filename)
f.writelines(lines)
f.close()
____________________________________________________________________

Writing directly to the line number would be ideal.
Some thing like: f.write(line number, string) if there is a function
like that.

Or would the best way to do line replacement be through iteration.
__________________________________________________________________

for line in open(filename):
# write to current line???

_________________________________________________________________

Thank you.

Best regards,

Tak




More information about the Tutor mailing list