file opening and closing

Discussion in 'Python' started by hokieghal99, Dec 24, 2003.

  1. hokieghal99

    hokieghal99 Guest

    jpg = string.find(file(os.path.join(root,fname), 'rb').read(), 'JFIF')

    Should this file be closed after reading? If so, how would one close it?
    What's the downside to not closing it?
    hokieghal99, Dec 24, 2003
    #1
    1. Advertising

  2. hokieghal99

    anton muhin Guest

    hokieghal99 wrote:
    > jpg = string.find(file(os.path.join(root,fname), 'rb').read(), 'JFIF')
    >
    > Should this file be closed after reading? If so, how would one close it?
    > What's the downside to not closing it?
    >


    It would, then the file object gets destroyed (it method __del__ is
    called). However, as Python uses garbage collection, it can happen much
    later after you leave the function.

    There is mostly no downsides except for the cases when you try to read
    this file---it might not be flashed.

    regards,
    anton.
    anton muhin, Dec 24, 2003
    #2
    1. Advertising

  3. hokieghal99

    Donn Cave Guest

    Quoth anton muhin <>:
    | hokieghal99 wrote:
    |> jpg = string.find(file(os.path.join(root,fname), 'rb').read(), 'JFIF')
    |>
    |> Should this file be closed after reading? If so, how would one close it?
    |> What's the downside to not closing it?
    |
    | It would, then the file object gets destroyed (it method __del__ is
    | called). However, as Python uses garbage collection, it can happen much
    | later after you leave the function.
    |
    | There is mostly no downsides except for the cases when you try to read
    | this file---it might not be flashed.

    (Flushed, that is, and of course that wouldn't be a problem here since
    the file is only to be read.) Note that the exact behavior here depends
    on the implementation: Java Python may as you say defer the close
    indefinitely, but C Python will execute it immediately on function return,
    because its garbage collection is based on reference count.

    The above usage is fine in C Python. In Java Python, it's probably OK
    but potential problems would include file descriptor resource starvation
    if opening many files this way, and low disk space if reading large files
    this way and then deleting them, because the open file descriptor will
    preserve the actual file even though its directory entry is gone.

    Donn Cave,
    Donn Cave, Dec 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nemesis
    Replies:
    0
    Views:
    524
    Nemesis
    Oct 6, 2005
  2. Nemesis
    Replies:
    2
    Views:
    1,379
  3. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    641
    Thomas Hansen
    Feb 15, 2007
  4. Saravan Wants
    Replies:
    3
    Views:
    3,676
    Peter Flynn
    Sep 30, 2010
  5. thomas
    Replies:
    0
    Views:
    215
    thomas
    Oct 23, 2003
Loading...

Share This Page