how do you know if open failed?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SpreadTooThin

    #1

    how do you know if open failed?

    f = open('myfile.bi n', 'rb')

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

  • tobiah

    #2
    Re: how do you know if open failed?

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

    --
    Posted via a free Usenet account from http://www.teranews.com

    Comment

    • Fredrik Lundh

      #3
      Re: how do you know if open failed?

      SpreadTooThin wrote:
      f = open('myfile.bi n', 'rb')
      >
      How do I know if there was an error opening my file?
      you'll notice:
      >>f = open("myfile.bi n", "rb")
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      IOError: [Errno 2] No such file or directory: 'myfile.bin'
      >>>
      </F>

      Comment

      • Neil Cerutti

        #4
        Re: how do you know if open failed?

        On 2006-09-28, SpreadTooThin <bjobrien62@gma il.comwrote:
        f = open('myfile.bi n', '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.bi n', 'rb')
        # Do stuff with f
        except IOError, inst:
        print 'Phooey.', inst.errno, inst.strerror

        --
        Neil Cerutti

        Comment

        • Erik Johnson

          #5
          Re: how do you know if open failed?


          "tobiah" <toby@tobiah.or gwrote in message
          news:451c0669$0 $19689$88260bb3 @free.teranews. com...
          SpreadTooThin wrote:
          f = open('myfile.bi n', 'rb')

          How do I know if there was an error opening my file?
          try:
          open('noexist')
          except:
          print "Didn't open"
          That's a way to trap any exception. I think a better answer to the
          question is "You'll know if it didn't work because Python throws exceptions
          when it runs into problems." You can catch exceptions and try to do
          something about them if you want to. Uncaught exceptions cause the
          interpreter to exit with a stack trace. Sometimes that's the most logical
          thing to do.
          >>fd = open('doesnt_ex ist', 'rb')
          Traceback (most recent call last):
          File "<stdin>", line 1, in ?
          IOError: [Errno 2] No such file or directory: 'doesnt_exist'


          It would throw a different exception if there were a permission problem, for
          example.

          -ej


          Comment

          • Bruno Desthuilliers

            #6
            Re: how do you know if open failed?

            tobiah a écrit :
            SpreadTooThin wrote:
            >
            >f = open('myfile.bi n', '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

            Comment

            Working...