By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

File modes

P: n/a

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

May 10 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
May 10 '07 #2

P: n/a
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.

May 11 '07 #3

P: n/a
En Thu, 10 May 2007 21:11:16 -0300, Jon Pentland <J2*****@gmail.com>
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

May 11 '07 #4

P: n/a
On May 10, 7:11 pm, Jon Pentland <J2Th...@gmail.comwrote:
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

May 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.