473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

File modes


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
4 2040
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
0
by: pxlpluker | last post by:
What exactly does the underlined text mean. w & a obviously mean you are going to update the file. So what does the + mean on w+ & a+ mean? Also please could someone explain w+ truncating the...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
1
by: Phil S | last post by:
When opening a file in two different threads at the same time, it will cause an exception if the FileShare modes are incompatible with the FileAccess modes. Is there some way to test whether...
6
by: Jakob Bieling | last post by:
Hi, I was wondering why there are file access modes, which you have to specify when opening the file. I am specifically talking about the 'read', 'read-write' and 'write' distinction. As far as...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
5
by: erikcw | last post by:
Hi all, I've created a script that reads in a file, replaces some data (regex), then writes the new data back to the file. At first I was convinced that "w+" was the tool for the job. But now...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.