[Tutor] sockets, files, threads

Alan Gauld alan.gauld at freenet.co.uk
Thu Jan 13 10:21:32 CET 2005


> Where did my 'ooo' go?
> 
> #! /usr/bin/env python
> import os
> 
> fobj = open('/tmp/xxx','w')
> fobj.write('ooo\n')

fobj.flush()

File objects use buffered IO and by changing from using file 
objects to file descriptors half way through the buffer
could be in any state. You need to flush() to force it to 
the file before doing anything weird.

As was said in a recent thread, mixing file objects and 
file descriptors is a bad idea. The OS can get confused, 
let alone the programmer!

> os.close(fp)

And especially if you open the file as one thing and 
close it as another! Be consistent, either work with 
file descriptors throughout or file objects. If you 
must mix them be prepared for strangeness.

Alan G.


More information about the Tutor mailing list