how do you know if open failed?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Sep 28 15:23:25 EDT 2006


tobiah a écrit :
> SpreadTooThin wrote:
> 
>> f = open('myfile.bin', 'rb')
>>
>> How do I know if there was an error opening my file?
>>
> try:
>         open('noexist')
> except:
>         print "Didn't open"
> 

Should be:

try:
   f = open('noexists')
except IOError, e:
   print >> sys.stderr, "Failed to open 'noexists' : %s" % e




More information about the Python-list mailing list