Catching a specific IO error

Discussion in 'Python' started by Tina I, Apr 24, 2007.

  1. Tina I

    Tina I Guest

    Hi group :)

    I have this standard line:

    export = open(self.exportFileName , 'w')

    'exportFileName' is a full path given by the user. If the user gives an
    illegal path or filename the following exception is raised:
    "IOError: [Errno 2] No such file or directory: /some/path/file.txt"

    So at the moment I do this:

    try:
    export = open(self.exportFileName , 'w')
    export.write("Something")
    export.close()
    except IOError:
    # calling an error handling method.

    Now, this works but of course it catches every IOError, and I can not
    figure out how to restrict it to only catch the "[Errno 2]"?

    Thanks
    Tina
     
    Tina I, Apr 24, 2007
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Thomas_Kr=FCger?=, Apr 24, 2007
    #2
    1. Advertising

  3. En Tue, 24 Apr 2007 08:44:05 -0300, Tina I <>
    escribió:

    > Hi group :)
    >
    > I have this standard line:
    >
    > export = open(self.exportFileName , 'w')
    >
    > 'exportFileName' is a full path given by the user. If the user gives an
    > illegal path or filename the following exception is raised:
    > "IOError: [Errno 2] No such file or directory: /some/path/file.txt"
    >
    > So at the moment I do this:
    >
    > try:
    > export = open(self.exportFileName , 'w')
    > export.write("Something")
    > export.close()
    > except IOError:
    > # calling an error handling method.
    >
    > Now, this works but of course it catches every IOError, and I can not
    > figure out how to restrict it to only catch the "[Errno 2]"?


    You can get the 2 as the errno exception attribute. BTW, 2 == errno.ENOENT

    try:
    export = open(self.exportFileName , 'w')
    except IOError, e:
    if e.errno==errno.ENOENT:
    # handle the "No such file or directory" error
    # calling an error handling method.

    See http://docs.python.org/lib/module-exceptions.html

    --
    Gabriel Genellina
     
    Gabriel Genellina, Apr 24, 2007
    #3
  4. Tina I

    Steve Holden Guest

    Thomas Krüger wrote:
    > Tina I schrieb:
    >> Now, this works but of course it catches every IOError, and I can not
    >> figure out how to restrict it to only catch the "[Errno 2]"?

    >
    > There's an example that uses the error number:
    > http://docs.python.org/tut/node10.html#SECTION0010300000000000000000
    >

    So what you'll need to do is catch all IOError exceptions, then test to
    see if you've got (one of) the particular one(s) you are interested in.
    If not then you can re-raise the same error with a bare "raise"
    statement, and any containing exception handlers will be triggered. If
    there are none then you will see the familiar traceback termination message.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com
     
    Steve Holden, Apr 24, 2007
    #4
  5. Tina I

    Steven Howe Guest

    Steve Holden wrote:
    > Thomas Krüger wrote:
    >
    >> Tina I schrieb:
    >>
    >>> Now, this works but of course it catches every IOError, and I can not
    >>> figure out how to restrict it to only catch the "[Errno 2]"?
    >>>

    >> There's an example that uses the error number:
    >> http://docs.python.org/tut/node10.html#SECTION0010300000000000000000
    >>
    >>

    > So what you'll need to do is catch all IOError exceptions, then test to
    > see if you've got (one of) the particular one(s) you are interested in.
    > If not then you can re-raise the same error with a bare "raise"
    > statement, and any containing exception handlers will be triggered. If
    > there are none then you will see the familiar traceback termination message.
    >
    > regards
    > Steve
    >

    you could also use some pre-testing of the filename os.path.isfile,
    os.path.isdir, os.path.split are good
    functions to test file/directory existence. Also to verify that you have
    permission to manipulate a file, os.access is a good function.
    sph
     
    Steven Howe, Apr 24, 2007
    #5
  6. Steven Howe wrote:
    > Steve Holden wrote:
    >> Thomas Krüger wrote:
    >>
    >>> Tina I schrieb:
    >>>
    >>>> Now, this works but of course it catches every IOError, and I can not
    >>>> figure out how to restrict it to only catch the "[Errno 2]"?
    >>>>
    >>> There's an example that uses the error number:
    >>> http://docs.python.org/tut/node10.html#SECTION0010300000000000000000
    >>>
    >>>

    >> So what you'll need to do is catch all IOError exceptions, then test
    >> to see if you've got (one of) the particular one(s) you are interested
    >> in. If not then you can re-raise the same error with a bare "raise"
    >> statement, and any containing exception handlers will be triggered. If
    >> there are none then you will see the familiar traceback termination
    >> message.
    >>
    >> regards
    >> Steve
    >>

    > you could also use some pre-testing of the filename os.path.isfile,
    > os.path.isdir, os.path.split are good
    > functions to test file/directory existence. Also to verify that you have
    > permission to manipulate a file, os.access is a good function.


    The try first approach is better for at least two reasons:

    1) It saves you an extra stat() on the disk, which can be really
    important for some filesystems I use :)

    2) It is atomic. If os.path.isfile() returns True but the file is
    deleted before you open it, you are still going to have to handle the
    exception.
    --
    Michael Hoffman
     
    Michael Hoffman, Apr 24, 2007
    #6
  7. Tina I

    John Machin Guest

    On 25/04/2007 4:06 AM, Steven Howe wrote:
    > Steve Holden wrote:
    >> Thomas Krüger wrote:
    >>
    >>> Tina I schrieb:
    >>>
    >>>> Now, this works but of course it catches every IOError, and I can not
    >>>> figure out how to restrict it to only catch the "[Errno 2]"?
    >>>>
    >>> There's an example that uses the error number:
    >>> http://docs.python.org/tut/node10.html#SECTION0010300000000000000000
    >>>
    >>>

    >> So what you'll need to do is catch all IOError exceptions, then test
    >> to see if you've got (one of) the particular one(s) you are interested
    >> in. If not then you can re-raise the same error with a bare "raise"
    >> statement, and any containing exception handlers will be triggered. If
    >> there are none then you will see the familiar traceback termination
    >> message.
    >>
    >> regards
    >> Steve
    >>

    > you could also use some pre-testing of the filename os.path.isfile,
    > os.path.isdir, os.path.split are good
    > functions to test file/directory existence.


    In general, this is laborious, tedious, and possibly even platform
    dependent. Then you still need to wrap the open call in try/accept. Why
    bother?

    In particular, (1) please explain how os.path.split helps with existence
    testing:

    Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    | >>> import os.path
    | >>> os.path.split(r'no\such\path\nothing.nix')
    ('no\\such\\path', 'nothing.nix')

    (2) please explain why you avoided mentioning os.path.exists.
     
    John Machin, Apr 25, 2007
    #7
  8. Tina I

    Tina I Guest

    Gabriel Genellina wrote:
    > You can get the 2 as the errno exception attribute. BTW, 2 == errno.ENOENT
    >
    > try:
    > export = open(self.exportFileName , 'w')
    > except IOError, e:
    > if e.errno==errno.ENOENT:
    > # handle the "No such file or directory" error
    > # calling an error handling method.
    >
    > See http://docs.python.org/lib/module-exceptions.html
    >
    > --Gabriel Genellina


    Perfect! Just what I was looking for. Thank you! :)

    Tina
     
    Tina I, Apr 25, 2007
    #8
    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. Ron Vecchi
    Replies:
    4
    Views:
    374
    Jacob Yang [MSFT]
    Nov 22, 2003
  2. Harlin Seritt

    Catching very specific exceptions

    Harlin Seritt, Jan 22, 2006, in forum: Python
    Replies:
    6
    Views:
    589
    Harlin Seritt
    Jan 22, 2006
  3. =?Utf-8?B?SmF2?=

    Is ViwState Page-Specific or UserControl-Specific

    =?Utf-8?B?SmF2?=, Aug 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    564
    =?Utf-8?B?SmF2?=
    Aug 16, 2006
  4. mazdotnet
    Replies:
    2
    Views:
    421
    Alexey Smirnov
    Oct 2, 2009
  5. Victor Hooi
    Replies:
    4
    Views:
    124
    Victor Hooi
    Oct 23, 2013
Loading...

Share This Page