473,394 Members | 4,037 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,394 software developers and data experts.

Misleading IOerror when opening a non-existent file for append?

jkn
Hi all

Python 2.4.2 (#1, Apr 26 2006, 23:35:31)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright", "credits" or "license" for more information.
>>ifile=open("\\no_such_dir\\no_such_file" ,'a')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: invalid mode: a
>>>
but:
>>ifile=open("\\no_such_dir\\no_such_file" ,'r')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory:
'\\no_such_dir\\no_such_file'
>>>
This 'invalid mode: a' error message seems weird to me. Is this a bug
or am I missimg something?

Thanks
Jon N

Nov 20 '06 #1
5 7651
It probably is misleading. But I'd say it's a misleading Linux message
not a misleading Python message.
f = file(r'c:\temp\DoesNotExist.txt','a')
not only does not give a misleading error under Windows, it works.

It appears that under Linux if you open a file for writing, but specify
append, it complains about the append if there isn't a file to append
to (a reasonable complaint, IMO).
>ifile=open("\\no_such_dir\\no_such_file" ,'a')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: invalid mode: a
>ifile=open("\\no_such_dir\\no_such_file" ,'r')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory:
Nov 20 '06 #2
pa**********@nibc.com wrote in news:1164037676.537263.53480
@f16g2000cwb.googlegroups.com in comp.lang.python:
It probably is misleading. But I'd say it's a misleading Linux message
not a misleading Python message.
f = file(r'c:\temp\DoesNotExist.txt','a')
not only does not give a misleading error under Windows, it works.
AFAICT that wasn't quite what the OP was trying to do, more like:
>>f = file(r'c:\does-not-exist\DoesNotExist.txt','a')
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
f = file(r'c:\does-not-exist\DoesNotExist.txt','a')
IOError: [Errno 2] No such file or directory: 'c:\\does-not-exist
\\DoesNotExist.txt'
>>>
So at least the error messsage is better.

But I'm confused by the use of '\\' by the OP on *nix this should
be just a character in the filename so in /home/username

file=open("\\no_such_dir\\no_such_file" ,'a')

whould be attempting to append to the (presumably) non-existant
file /home/username/\\no_such_dir\\no_such_file

Perhaps the append mode ('a') doens't create a file on the OP's
linux box, and thus using 'a' for a file name that doesn't exist
is an "invalid mode".

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 20 '06 #3
jkn
Hi there
Thanks for the comments. I see that I actually confused myself a
bit with my posting. It's been a while and I mistook the use of '/' as
a platform-independent directory delimiter with the use of '\\'.

I also thought that I was seeing something similar on Windows and
Linux. This may not be the case ;-o.

So, to try again. It looks as it the attempt to open a non-existent
file (for instance, because the parent folder does not exist) gives an
odd error on Linux but not on Windows.

On Windows:
>f = file(r'c:\does-not-exist\DoesNotExist.txt','a')

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
f = file(r'c:\does-not-exist\DoesNotExist.txt','a')
IOError: [Errno 2] No such file or directory: 'c:\\does-not-exist
\\DoesNotExist.txt'
>>
(replacing the '\' with '/' gives the same message, which is what I was
trying to convey)

On Linux, you get the 'Bad mode' error.

Strange?!

Jon N

Nov 21 '06 #4
On 2006-11-21, jkn <jk****@nicorp.f9.co.ukwrote:
Hi there
Thanks for the comments. I see that I actually confused myself a
bit with my posting. It's been a while and I mistook the use of '/' as
a platform-independent directory delimiter with the use of '\\'.

I also thought that I was seeing something similar on Windows and
Linux. This may not be the case ;-o.

So, to try again. It looks as it the attempt to open a non-existent
file (for instance, because the parent folder does not exist) gives an
odd error on Linux but not on Windows.

On Windows:
>>f = file(r'c:\does-not-exist\DoesNotExist.txt','a')

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
f = file(r'c:\does-not-exist\DoesNotExist.txt','a')
IOError: [Errno 2] No such file or directory: 'c:\\does-not-exist
\\DoesNotExist.txt'
>>>

(replacing the '\' with '/' gives the same message, which is what I was
trying to convey)

On Linux, you get the 'Bad mode' error.

Strange?!
Not on my box (Python 2.4.4c0):
>>open("nodir/nofile", "a")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'nodir/nofile'

--
Antoon Pardon
Nov 21 '06 #5
jkn wrote:
This 'invalid mode: a' error message seems weird to me. Is this a bug
or am I missimg something?
It depends on the underlying implementation of fopen(), which, on some
platforms, doesn't set the right error code for bad mode strings.
Python uses some simple heuristics to try to get around this (earlier
versions could give "error: no error" for this case), but they're not
always able to do the right thing.

</F>

Nov 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Debbie | last post by:
Hi I have developed an application that access an SQL database and installed it on a different machine The installation is successful but when I try to run the application I get the following...
116
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
1
by: wh | last post by:
Help! I need to know what permissions (NTFS?) I need to set when authoring a web application in VS.NET. I've previously been logging in as Administrator but since tightening up security on my...
4
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
3
by: Nicola | last post by:
When using the NewMail.Body object, ! and spaces are being added into the email after the email has been sent. Has anyone else had this problem or even better a solution??
21
by: alistair_henderson | last post by:
Morning All, I have some code for a website which uses 'window.open' to simulate modal dialog boxes. I use the window.closed property to decide if the window object exists at various points. ...
7
by: Marco | last post by:
Hello,every one, I meet a question: in my old script, I usually use os.popen2() to get info from standard unix(LinuX) program like ps,ifconfig... Now, I write a OO-based programme, I still use...
1
by: Tim Couper | last post by:
As you know, when an IOError is raised, you get a helpful: <useful descriptor of the error> Is there a list of all possible values of N which can be returned, and their meanings? -- Dr...
0
by: YouOverThere | last post by:
I keep getting the error message "IOError: decoder zip not available" when using the Python Imaging Library. The error has occurred when trying to work with PNGs or JPEGs or when loading the fonts...
2
DonRayner
by: DonRayner | last post by:
This one has me stumped. I'm getting a "Type Mismatch" error on one of my forms when it's being opened. It's hapening before the forms "On Open" event, I stuck a msgbox in there to check and I'm...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.