[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


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()
# num for some line number
line[num] = "String"
f = open(filename)

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,


More information about the Tutor mailing list