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

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.