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

J

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.Traceback (most recent call last):
File said:
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
 
P

paul.keating

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).
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: invalid mode: a Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory:
 
R

Rob Williscroft

(e-mail address removed) wrote in @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:

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

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.

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
 
A

Antoon Pardon

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.

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):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'nodir/nofile'
 
F

Fredrik Lundh

jkn said:
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>
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top