what exceptions may file() and read() throw?

wittempj@hotmail.com martin.witte at gmail.com
Fri Jun 23 11:38:38 EDT 2006


Daniel Schüle wrote:
> Hello,
>
> currently I am using this instance method
>
>         def getFilecontent(self, filename):
>                 try:
>                         return file(filename).read()
>                 except IOError, err_msg:
>                         print err_msg
>                         sys.exit(1)
>                 except:
>                         print "unknown exception in PackageParser"
>                         sys.exit(1)
>
> I tried to open a file for which I don't have the permissions to read
> (etc/shadow)
> and I tried to open a file which doesn't exist
> in both cases I got IOError exception, so my question is
> does it make sence to have
>
>                 except:
>                         print "unknown exception in PackageParser"
>                         sys.exit(1)
>
> or is it a dead code?
> are there some good reference sources to see what file() and read()
> may throw, IMHO it's OS dependent.
>
> Regards, Daniel

You also could do something like

       def getFilecontent(self, filename):
                try:
                        return file(filename).read()
                except IOError, err_msg:
                        print err_msg
                        sys.exit(1)
                except Exception ,e:
                        print e.__class__, str(e)

Then the next tiome this happens you have more information on what
happened




More information about the Python-list mailing list