File modes

Discussion in 'Python' started by HMS Surprise, May 10, 2007.

  1. HMS Surprise

    HMS Surprise Guest

    After reading a file is it possible to write to it without first
    closing it? I tried opening with 'rw' access and re-winding. This does
    not seem to work unless comments are removed.

    Also, does close force a flush?

    Thanks,

    jh

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    f = open('c:\\tempMaxq\\incidents.txt', 'rw')
    s = f.read()
    lst = s.split()
    incId = []
    incId.extend([lst.pop(), lst.pop()])
    #f.close()
    #f = open('c:\\tempMaxq\\incidents.txt', 'w')
    #f.seek(0)
    for el in lst:
    f.write(el + ' ')
    f.close()
     
    HMS Surprise, May 10, 2007
    #1
    1. Advertising

  2. > After reading a file is it possible to write to it without first
    > closing it? I tried opening with 'rw' access and re-winding. This does
    > not seem to work unless comments are removed.
    >
    >
    > Also, does close force a flush?
    >
    > Thanks,
    >
    > jh
    >
    > #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > f = open('c:\\tempMaxq\\incidents.txt', 'rw')
    > s = f.read()
    > lst = s.split()
    > incId = []
    > incId.extend([lst.pop(), lst.pop()])
    > #f.close()
    > #f = open('c:\\tempMaxq\\incidents.txt', 'w')
    > #f.seek(0)
    > for el in lst:
    > f.write(el + ' ')
    > f.close()



    Please see the documentation of the function open( ):
    http://python.org/doc/lib/built-in-funcs.html It says that the modes
    can only be 'r', 'w', 'a', 'r+', 'w+', 'a+' and possibly a 'b' or 'U'
    appended to these. So if you open it with 'rw' it will be interpreted
    as 'r'. For example this will not work:

    f = open( 'myfile', 'rw' )
    f.write( 'hello' )
    f.close( )

    because python thinks you want to open 'myfile' in 'r' mode. I guess I
    agree that the thrown exception IOError: [Errno 9] Bad file descriptor
    is not very informative in this case.

    HTH,
    Daniel
     
    Daniel Nogradi, May 10, 2007
    #2
    1. Advertising

  3. HMS Surprise

    Jon Pentland Guest

    I don't really see the use for being able to do that. Have you tried
    doing it with the 'app' mode?, But I am guessing that it is just an
    advanced mode spawned from 'w'. So, no, I don't think you can do this.
     
    Jon Pentland, May 11, 2007
    #3
  4. En Thu, 10 May 2007 21:11:16 -0300, Jon Pentland <>
    escribió:

    > I don't really see the use for being able to do that. Have you tried
    > doing it with the 'app' mode?, But I am guessing that it is just an
    > advanced mode spawned from 'w'. So, no, I don't think you can do this.


    In fact you can read and write the same file, using the r+/w+/a+ modes.
    You may need a seek() when switching from reading to writing or viceversa
    (unless mode is a+ perhaps), but I can't find that in the docs; perhaps
    Python itself already takes care of this internally.

    --
    Gabriel Genellina
     
    Gabriel Genellina, May 11, 2007
    #4
  5. HMS Surprise

    HMS Surprise Guest

    On May 10, 7:11 pm, Jon Pentland <> wrote:
    > I don't really see the use for being able to do that. Have you tried
    >


    Well, I think I found a reason and it probably happens quite a bit.

    I open the file and read it into a list. I pop some elements from the
    list for processing and then write the shortened list back to disk to
    be available for other functions to access later, where later varies
    from seconds to days. There is no need to keep the file open till
    after the processing so I wish to write/flush/close right away.

    jvh
     
    HMS Surprise, May 11, 2007
    #5
    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. Jakob Bieling

    Q: Why are there file access modes?

    Jakob Bieling, Dec 2, 2003, in forum: C++
    Replies:
    6
    Views:
    377
    jeffc
    Dec 3, 2003
  2. simon place
    Replies:
    5
    Views:
    367
    Michael Hudson
    Aug 27, 2003
  3. erikcw

    Having trouble with file modes

    erikcw, Nov 3, 2006, in forum: Python
    Replies:
    6
    Views:
    291
    Fredrik Lundh
    Nov 4, 2006
  4. tubby
    Replies:
    2
    Views:
    323
    Stefan Schwarzer
    Jan 7, 2007
  5. Martin DeMello

    file modes

    Martin DeMello, Jul 10, 2008, in forum: Ruby
    Replies:
    5
    Views:
    127
    Jens Wille
    Jul 10, 2008
Loading...

Share This Page