Exceptions when closing a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven D'Aprano

    #1

    Exceptions when closing a file

    Closing a file can (I believe) raise an exception. Is that documented
    anywhere? I've spent a lot of frustrating time trying to track this down,
    with no luck, which suggests that either my google-foo is weak or that it
    isn't documented. Is IOError the only exception it can raise?

    The only thing I have found is this:



    Out of curiosity, is there a simple way to demonstrate close() raising an
    exception that doesn't involve messing about with disk quotas?


    --
    Steven.

  • kyosohma@gmail.com

    #2
    Re: Exceptions when closing a file

    On Mar 20, 12:25 pm, Steven D'Aprano
    <s...@REMOVE.TH IS.cybersource. com.auwrote:
    Closing a file can (I believe) raise an exception. Is that documented
    anywhere? I've spent a lot of frustrating time trying to track this down,
    with no luck, which suggests that either my google-foo is weak or that it
    isn't documented. Is IOError the only exception it can raise?
    >
    The only thing I have found is this:
    >
    http://mail.python.org/pipermail/pyt...November/02603...
    >
    Out of curiosity, is there a simple way to demonstrate close() raising an
    exception that doesn't involve messing about with disk quotas?
    >
    --
    Steven.
    I've never had any problems closing a file. Maybe you need to flush it
    before you close it? Are you running threads that access the file in
    ad hoc fashion or something else out of the ordinary? Is this some
    sort of long running process writing a large file?

    Mike

    Comment

    • Ross Ridge

      #3
      Re: Exceptions when closing a file

      Steven D'Aprano <steve@REMOVE.T HIS.cybersource .com.auwrote:
      >Closing a file can (I believe) raise an exception. Is that documented
      >anywhere?
      In a catch-all statement for file objects: "When a file operation fails
      for an I/O-related reason, the exception IOError is raised." The fact
      that close() is a file operation that might fail is revealed by "file
      objects are implemented using C's stdio package" and the fact the C's
      fclose() function can fail.
      Is IOError the only exception it can raise?
      I assume it can raise the exceptions MemoryError and KeyboardInterup t,
      which just about any Python operation can raise.
      >Out of curiosity, is there a simple way to demonstrate close() raising an
      >exception that doesn't involve messing about with disk quotas?
      Something like the following should work:

      f = file("/dev/null", "r")
      os.close(f.file no)
      f.close()

      Normally however, you can expect file method close() to fail for all
      the same reasons you would expect write() to fail.

      Ross Ridge

      --
      l/ // Ross Ridge -- The Great HTMU
      [oo][oo] rridge@csclub.u waterloo.ca
      -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
      db //

      Comment

      • John Nagle

        #4
        Re: Exceptions when closing a file

        Ross Ridge wrote:
        Steven D'Aprano <steve@REMOVE.T HIS.cybersource .com.auwrote:
        >
        >>Closing a file can (I believe) raise an exception. Is that documented
        >>anywhere?
        >
        >
        In a catch-all statement for file objects: "When a file operation fails
        for an I/O-related reason, the exception IOError is raised." The fact
        that close() is a file operation that might fail is revealed by "file
        objects are implemented using C's stdio package" and the fact the C's
        fclose() function can fail.
        >
        >
        >>Is IOError the only exception it can raise?
        Closing a file that's being written can, of course, fail.
        An I/O error is possible as the file is flushed to disk.

        This is useful; after the close has returned, you have
        some confidence that the file has been fully written.

        If you want to force this error, write to a drive
        reached over a network, or a removable medium like a floppy
        or flash card. Open a file for writing and disconnect the
        network or remove the removable medium.

        John Nagle

        Comment

        • Bruno Desthuilliers

          #5
          Re: Exceptions when closing a file

          kyosohma@gmail. com a écrit :
          On Mar 20, 12:25 pm, Steven D'Aprano
          <s...@REMOVE.TH IS.cybersource. com.auwrote:
          >
          >>Closing a file can (I believe) raise an exception. Is that documented
          >>anywhere? I've spent a lot of frustrating time trying to track this down,
          >>with no luck, which suggests that either my google-foo is weak or that it
          >>isn't documented. Is IOError the only exception it can raise?
          >>
          >>The only thing I have found is this:
          >>
          >>http://mail.python.org/pipermail/pyt...November/02603...
          >>
          >>Out of curiosity, is there a simple way to demonstrate close() raising an
          >>exception that doesn't involve messing about with disk quotas?
          >>
          >>--
          >>Steven.
          >
          >
          I've never had any problems closing a file. Maybe you need to flush it
          before you close it?
          Usually, closing the file flushes the buffer. At least with buffered
          files, but that's the most common situation AFAICT.
          Are you running threads that access the file in
          ad hoc fashion or something else out of the ordinary? Is this some
          sort of long running process writing a large file?
          Might just be that there's no more available storage space.

          Comment

          • kyosohma@gmail.com

            #6
            Re: Exceptions when closing a file

            On Mar 20, 4:26 pm, Bruno Desthuilliers
            <bdesth.quelque ch...@free.quel quepart.frwrote :
            kyoso...@gmail. com a écrit :
            >
            >
            >
            On Mar 20, 12:25 pm, Steven D'Aprano
            <s...@REMOVE.TH IS.cybersource. com.auwrote:
            >
            >Closing a file can (I believe) raise an exception. Is that documented
            >anywhere? I've spent a lot of frustrating time trying to track this down,
            >with no luck, which suggests that either my google-foo is weak or that it
            >isn't documented. Is IOError the only exception it can raise?
            >
            >The only thing I have found is this:
            >>
            >Out of curiosity, is there a simple way to demonstrate close() raising an
            >exception that doesn't involve messing about with disk quotas?
            >
            >--
            >Steven.
            >
            I've never had any problems closing a file. Maybe you need to flush it
            before you close it?
            >
            Usually, closing the file flushes the buffer. At least with buffered
            files, but that's the most common situation AFAICT.
            >
            Are you running threads that access the file in
            ad hoc fashion or something else out of the ordinary? Is this some
            sort of long running process writing a large file?
            >
            Might just be that there's no more available storage space.
            Very true...I was fishing for more information to be able to give a
            more specific answer. And I was tired, so I didn't think of
            everything. Good call, you guys!

            Mike

            Comment

            • Ben Finney

              #7
              Re: Exceptions when closing a file

              John Nagle <nagle@animats. comwrites:
              If you want to force [an error while closing a file], write to
              a drive reached over a network, or a removable medium like a floppy
              or flash card. Open a file for writing and disconnect the network
              or remove the removable medium.
              Or simply use a mock library, create a mock file object, and guarantee
              (for the purpose of your unit test) that it *will* raise the
              appropriate error without interacting with the rest of the system.

              --
              \ "Ours is a world where people don't know what they want and are |
              `\ willing to go through hell to get it." -- Donald Robert Perry |
              _o__) Marquis |
              Ben Finney

              Comment

              Working...