how do you know if open failed?

Neil Cerutti horpner at yahoo.com
Thu Sep 28 14:33:49 EDT 2006


On 2006-09-28, SpreadTooThin <bjobrien62 at gmail.com> 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



More information about the Python-list mailing list