Writing to a certain line?
bruno at modulix
onurb at xiludom.gro
Wed Jun 7 05:10:14 EDT 2006
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 !-)
Seriously : a for loop can only become an infinite loop if the iterable
is infinite. AFAIK, file objects (created from regular files on a
standard filesystem) are definitively not infinite.
Problem is elsewhere. But since you prefered to guess - instead of
providing relevant informations - we just can't help.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list