file opening and closing
Donn Cave
donn at drizzle.com
Thu Dec 25 00:20:55 EST 2003
More information about the Python-list mailing list
Thu Dec 25 00:20:55 EST 2003
- Previous message (by thread): file opening and closing
- Next message (by thread): Simple or Impossible in Python... dynamically dereferencing a class attribute
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Quoth anton muhin <antonmuhin at rambler.ru>: | 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 at drizzle.com
- Previous message (by thread): file opening and closing
- Next message (by thread): Simple or Impossible in Python... dynamically dereferencing a class attribute
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list