File modes

H

HMS Surprise

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()
 
D

Daniel Nogradi

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
 
J

Jon Pentland

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

Gabriel Genellina

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

HMS Surprise

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
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top