[Tutor] File mode r+
bob
bgailer at alum.rpi.edu
Sat Sep 24 18:03:59 CEST 2005
At 01:11 AM 9/24/2005, Shitiz Bansal wrote:
>Hi,
>I want to update a textfile using the r+ file mode.
>contents of file:
>
>abcd
>efgh
>ijkl
>mnop
>qrst
>uvwx
>yx12
>
>my scripts is:
>
>file1=open("aa.txt",'r+')
Instead of readline, use skip to position the file to where you want to
overwrite it.
file1.seek(10)
>file1.readline()
>file1.readline()
>file1.write("1234\n")
>file1.close()
>
>This should replace the third line with 1234.
>However it does nothing.
>
>Moreover the script:
>
>file1=open("aa.txt",'r+')
>file1.write("1234\n")
>file1.close()
>
>does replace the first line with 1234.
>
>could anyone explain what is happening?
>
>shitiz
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam? Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list