file opening and closing

anton muhin antonmuhin at rambler.ru
Wed Dec 24 11:39:01 EST 2003


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.




More information about the Python-list mailing list