Writing to a certain line?

Christophe chris.cavalaria at free.fr
Wed Jun 7 05:35:53 EDT 2006


bruno at modulix a écrit :
> Tommy B wrote:
> 
>>bruno at modulix wrote:
> 
> 
> (snip)
> 
> 
>>>import os
>>>old = open("/path/to/file.txt", "r")
>>>new = open("/path/to/new.txt", "w")
>>>for line in old:
>>> if line.strip() == "Bob 62"
>>>   line = line.replace("62", "66")
>>> new.write(line)
>>>old.close()
>>>new.close()
>>>os.rename("/path/to/new.txt", "/path/to/file.txt")
>>>
> 
> (snip)
> 
>>Umm... I tried using this method and it froze. Infiinite loop, I'm
>>guessing.
> 
> 
> Wrong guess - unless, as Fredrik suggested, you have an infinite disk
> with an infinite file on it. If so, please share with, we would be
> *very* interested !-)

Use /dev/zero as source and /dev/null as destination :D



More information about the Python-list mailing list