473,791 Members | 3,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:\\tempM axq\\incidents. txt', 'rw')
s = f.read()
lst = s.split()
incId = []
incId.extend([lst.pop(), lst.pop()])
#f.close()
#f = open('c:\\tempM axq\\incidents. txt', 'w')
#f.seek(0)
for el in lst:
f.write(el + ' ')
f.close()

May 10 '07 #1
4 2052
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:\\tempM axq\\incidents. txt', 'rw')
s = f.read()
lst = s.split()
incId = []
incId.extend([lst.pop(), lst.pop()])
#f.close()
#f = open('c:\\tempM axq\\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
10142
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 this while experimenting, trying to figure out the file objects modes,
0
1335
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 file. I may just be dense so please be patient :) ------------------------------------------------------------------------- file( filename ] )
8
9859
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
1
1274
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 access will be allowed to the file without having to try opening it and get an exception?
6
2066
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 I can tell, it only makes sure, you (the programmer) do not accidently write to it when you opened it for reading. To me, this seems like another one of those things that just make life harder. I am asking this, because I am writing a wrapper...
4
9844
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 = tmpfile(); /* write into tmpFile */ ...
68
5262
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
13
11156
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 writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
5
1307
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 I'm finding that the contents of the file are deleted (so I can't read the data in). f = open('_i_defines.php', 'w+')
10
22755
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) outFile.write("teststring", 10); outFile.close(); If I leave out the ios::ate and ios::app modes my string is written to the start of the file as I'd expect but I want to write the data to
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.