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

Discussion in 'Python' started by jkn, Nov 20, 2006.

  1. jkn

    jkn Guest

    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
    jkn, Nov 20, 2006
    #1
    1. Advertising

  2. jkn

    Guest

    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, 2006
    #2
    1. Advertising

  3. 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/
    Rob Williscroft, Nov 20, 2006
    #3
  4. jkn

    jkn Guest

    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
    jkn, Nov 21, 2006
    #4
  5. On 2006-11-21, jkn <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
    Antoon Pardon, Nov 21, 2006
    #5
  6. 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>
    Fredrik Lundh, Nov 22, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul W

    Non-existent datagrid?

    Paul W, Nov 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    323
    Paul W
    Nov 10, 2004
  2. Claudio Grondi
    Replies:
    8
    Views:
    780
    Georg Brandl
    Aug 28, 2006
  3. Les Caudle
    Replies:
    1
    Views:
    340
    Cowboy \(Gregory A. Beamer\)
    Sep 19, 2006
  4. JT Kimbell
    Replies:
    3
    Views:
    117
    JT Kimbell
    Jun 13, 2007
  5. Hagbard Celine
    Replies:
    4
    Views:
    106
    Hagbard Celine
    Sep 25, 2010
Loading...

Share This Page