Diff between opening files in 'r' and 'r+' mode

Sick Monkey sickcodemonkey at gmail.com
Tue Jan 30 08:49:47 EST 2007


It was a little hard to follow your logic of your sample code (writing,
reading and writing again), but
(1)The difference between r and r+.....
-  'r+' opens the file for both reading and writing.
-  'r' should be used when the file will only be read.

I am not sure on how you want to store the contents of the file, but I have
provided an example below. You could do something like this:
----------------------------------------------------------------------
fileContent = open(fname, 'r')

a = fileContent.readlines(); fileContent.close()

print a

----------------------------------------------------------------------

(2) If you are just going to read the file, then yes I would use just the
'r' argument.



On 30 Jan 2007 01:36:15 -0800, raghu <vgvr620034 at gmail.com> wrote:
>
> i want to know the difference between 'r' mode and 'r+' mode
> 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())?
> Before going to next question [I deleted all the contents in the text
> file]
>
> 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
>                          2.Should i recall again the opening of the
> file in 'r' mode to read the file?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070130/cb759681/attachment.html>


More information about the Python-list mailing list