Re: how do you know if open failed?



On 2006-09-28, SpreadTooThin <bjobrien62@xxxxxxxxx> wrote:
f = open('myfile.bin', 'rb')

How do I know if there was an error opening my file?

Try it an see.

Seriously, it will raise an exception that you can catch.

try:
f = open('myfile.bin', 'rb')
# Do stuff with f
except IOError, inst:
print 'Phooey.', inst.errno, inst.strerror

--
Neil Cerutti
.



Relevant Pages

  • Re: Exceptions and out procedure arguments (using GNAT GPL)
    ... raise My_Exception; ... and has an exception handler ... you can count on any assignments that you've already done ... The sample is a highly condensed version of my Query procedure ...
    (comp.lang.ada)
  • Re: contracted exceptions
    ... silently dies -- without notifying anyone. ... Enforcing the subprograms ... superior to the existing rule of silently ignoring the exception. ... Or raise Program_Error in the parent task at the point where that task ...
    (comp.lang.ada)
  • Re: ascii to tword - help
    ... if(cmp128(inputL, ... Raise($1_1285); ... we succeeded if we got this exception. ...
    (alt.lang.asm)
  • Re: AssignFile, Reset und IOResult 103
    ... on exception do ... Raise ESecHashException.Create('File not found! ... Raise ESecHashException.CreateFmt('Could not open File ''%s''.'#13#13' ... Die beiden Dateinamen haben definitiv nichts miteinander zu tun. ...
    (de.comp.lang.delphi.misc)
  • C++ Design Principles for Ruby - Prefer Abstract Interfaces.
    ... raise "Pure virtual" end end ... class TypeCheckException < Exception ... class DuckTypingException < TypeCheckException ... class PolymorphicTypeException < TypeCheckException ...
    (comp.lang.ruby)