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

R

raghu

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?
 
B

Ben Finney

raghu said:
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]

This is amazingly hard to read. Can you please post your message
again, this time using ordinary whitespace (e.g. a blank line) to
separate program examples from other text.

All the punctuation characters you're using have typographical
meaning, and your arbitrary use of them for apparently decorative
purposes make it difficult to see what you're trying to day.
 
D

Dennis Lee Bieber

On 30 Jan 2007 01:36:15 -0800, "raghu" <[email protected]> declaimed
the following in comp.lang.python:

<nasty unreadable stuff snipped -- next time enter the examples into
the Python interpreter console and just cut&paste what it shows>
[MY QUESTION]:i want to read the text file contents cant it be done by
giving (print i.read())?

To use any of the "+" file modes, you need to perform a seek()
operation when changing from read to write (or the other way).
odd text
fr.seek(5)
fr.write("NEW")
fr.seek(0)
print fr.readlines() ['Some NEW text\n', "in two 'logical' lines\n"]
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top