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

Discussion in 'Python' started by raghu, Jan 30, 2007.

  1. raghu

    raghu Guest

    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?
     
    raghu, Jan 30, 2007
    #1
    1. Advertising

  2. raghu

    Ben Finney Guest

    "raghu" <> writes:

    > 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.

    --
    \ "Experience is that marvelous thing that enables you to |
    `\ recognize a mistake when you make it again." -- Franklin P. |
    _o__) Jones |
    Ben Finney
     
    Ben Finney, Jan 30, 2007
    #2
    1. Advertising

  3. On 30 Jan 2007 01:36:15 -0800, "raghu" <> 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).

    >>> fr = open("SomeTest.txt", "w+") #w+ as the file is new
    >>> fr.write("Some odd text\nin two 'logical' lines\n")
    >>> fr.seek(5)
    >>> print fr.readline()

    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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Jan 30, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    514
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    644
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,932
    Steve Fulton
    Aug 31, 2004
  4. Santa
    Replies:
    1
    Views:
    1,094
    Mark A. Odell
    Jul 17, 2003
  5. manu
    Replies:
    11
    Views:
    1,664
    Default User
    Jan 5, 2009
Loading...

Share This Page