[Tutor] File mode r+

Shitiz Bansal shitizb at yahoo.com
Sat Sep 24 22:44:31 CEST 2005


Hi,

How do i proceed if i am not sure about the number of characters in the fist two lines and want to write in the third line.is there a way to skip two lines and write on the third??

Also could anyone explain why the readline() did not work. according to what i understand it should.

 

shitiz


bob <bgailer at alum.rpi.edu> wrote:
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




		
---------------------------------
Yahoo! for Good
 Click here to donate to the Hurricane Katrina relief effort. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050924/3eb488e5/attachment.html


More information about the Tutor mailing list