[Tutor] Diff between opening files in 'r' and 'r+' mode

Alan Gauld alan.gauld at btinternet.com
Tue Jan 30 11:20:45 CET 2007


Hi Vanam,

I'm a wee bit confused by your email.

> i want to know the difference between 'r' mode and 'r+' mode

r+ allows you to both read and write to the file without having
to close in between. But its up to you to control where the
"cursor" is within the file so that you write in the correct places.

>>>>>>>>>>>
1.i = open('c:\python25\integer.txt','w')-------->for writiing
  i.write('hai')--------->written some content in text file
  i = open('c:\python25\integer.txt','r')---->for reading
  print i.read()---->for printing the contents in that text file
  i = open('c:\python25\integer.txt','w')---------->for writing
  i.write('how')-----------?Rewrite the contents
  print i.read()
[MY QUESTION]:i want to read the text file contents cant it be done by
giving (print i.read())?
>>>>>>>>>>>

Yes you can print i.read()
But in the sequence above you never close the file which may
result in strangeness. For example your writes may not actually
get sent to disk... It is always good practice to close a file before
trying to reopen it.

In the examples above, what are you expecting to see?
I asume 'hai' the first time and 'how' the second? But on the
second you don't even open i for reading following the write....

>>>>>>>>>>>>
2.i = open('c:\python25\integer.txt','r+')-----For reading and writing
   i.write('hai')--------->written some content  to text file
   print i.read()--------->{Ø
†('c:\python25\integer.txt','w')
                               i write('')
                               print i.read()how')
                               i = open('c:\python25\integer.txt','r')
                               print i.read()
                               i = open('c:\python25\integer.txt','w')
                               i.write()
                               i = open('c:\python25\integer.txt','r')
                              print i.read() } --->Thats what i saw on
interpreter(In curly braces) when  i ran the script
>>>>>>>>>>>>

> [MY QUESTION]:1.from where the above in curly braces
> is printed?and i have written only 'hai' to the text file

I have no idea!
What I would expect to happen is that you write 'hai' to the
start of the file then the read will read in the contents of the file,
except for the hai which you have written. But you may
need to use flush() to force the write first. But whether
the stuff above is the file contents I don't know. - what
happens if you open the file in a text editor?

> 2.Should i recall again the opening of the file in
> 'r' mode to read the file?

r+ should allow you to read but depending on what you need
you may need to position the cursor using seek() first. In general
I don't encourage the use of r+ because it does require careful
control of exactly where you are in the file, which can be difficult.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list