[Tutor] tempfile (fwd)

Alan Gauld alan.gauld at freenet.co.uk
Fri Dec 24 09:08:04 CET 2004


> gets an OSError: [Errno 9] Bad File Descriptor
> 
> on os.read(tmp_fp,10)
> 
> I'm thinking that, if I don't keep a reference to the file object,
> it gets automatically closed.
> 
> How pythonic.

Yes it is. After all that's how garbage collection works for 
all objects in Python, when nothing is using it, it gets 
deleted.

But on a general note it's usually a bad idea to mix file 
descriptor and file object references to the same file 
at the same time. Things can get very confused at the OS level!

Most things(everything?) you want to do with a file 
object can be done with a descriptor, albeit with 
a little more effort. Someone already showed you 
the lseek() function which saves you doing the 
conversion, I'd recommend keeping things in 
one format or the other.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


More information about the Tutor mailing list