Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Misleading IOerror when opening a non-existent file for append?

Reply
Thread Tools

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

 
 
jkn
Guest
Posts: n/a
 
      11-20-2006
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

 
Reply With Quote
 
 
 
 
paul.keating@nibc.com
Guest
Posts: n/a
 
      11-20-2006
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:


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      11-20-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
jkn
Guest
Posts: n/a
 
      11-21-2006
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

 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      11-21-2006
On 2006-11-21, jkn <(E-Mail Removed)9.co.uk> wrote:
> 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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-22-2006
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: file write IOError Invalid argument Gabriel Genellina Python 0 07-20-2009 05:54 AM
Misleading error message when opening a file (on Windows XP SP 2) Claudio Grondi Python 8 08-28-2006 08:12 PM
PIL problem: IOError: cannot identify image file h112211@gmail.com Python 2 08-20-2006 01:38 PM
file.readlines() and IOError exceptions Astan Chee Python 0 07-12-2006 12:04 AM
readlines() with large file raises: IOError: [Errno 12] Cannotallocate memory nicogrubert@arcor.de Python 0 11-22-2004 09:06 AM



Advertisments