|Marilyn Davis||Jan 13, 2005 2:04 am|
|Danny Yoo||Jan 13, 2005 2:31 am|
|Danny Yoo||Jan 13, 2005 2:41 am|
|Marilyn Davis||Jan 13, 2005 3:17 am|
|Danny Yoo||Jan 13, 2005 6:29 am|
|Alan Gauld||Jan 13, 2005 10:20 am|
|Marilyn Davis||Jan 15, 2005 11:19 pm|
|Marilyn Davis||Jan 16, 2005 3:12 am|
|Marilyn Davis||Jan 16, 2005 6:47 am|
|Danny Yoo||Jan 16, 2005 7:40 am|
|Marilyn Davis||Jan 17, 2005 5:02 am|
|Danny Yoo||Jan 18, 2005 10:51 am|
|Danny Yoo||Jan 18, 2005 7:24 pm|
|Marilyn Davis||Jan 19, 2005 2:32 am|
|Danny Yoo||Jan 19, 2005 8:12 am|
|Kent Johnson||Jan 19, 2005 12:35 pm|
|Marilyn Davis||Jan 19, 2005 8:57 pm|
|Marilyn Davis||Jan 19, 2005 9:13 pm|
|Danny Yoo||Jan 19, 2005 9:53 pm|
|Marilyn Davis||Jan 19, 2005 10:28 pm|
|Marilyn Davis||Jan 21, 2005 5:05 am|
|Subject:||[Tutor] sockets, files, threads|
|From:||Danny Yoo (dy...@hkn.eecs.berkeley.edu)|
|Date:||Jan 13, 2005 6:29:36 am|
On Wed, 12 Jan 2005, Marilyn Davis wrote:
I was looking at my use of file objects and file descriptors and I wrote this sample program and was very surprised by the result -- which makes me think there's something here that I don't understand. Where did my 'ooo' go?
#! /usr/bin/env python import os
fobj = open('/tmp/xxx','w') fobj.write('ooo\n') fp = fobj.fileno() os.write(fp,'x\n') os.close(fp)
Oh! Can you explain why you're mixing the low-level 'os.write()' and 'os.close()' stuff with the high-level file methods?
The 'os' functions work at a different level of abstraction than the file object methods, so there's no guarantee that:
will do the proper flushing of the file object's internal character buffers.
Try this instead:
### fobj = open('/tmp/xxx','w') fobj.write('ooo\n') fobj.write('x\n') fobj.close() ###
The documentation on os.write() says:
"""Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by open() or pipe(). To write a ``file object'' returned by the built-in function open() or by popen() or fdopen(), or sys.stdout or sys.stderr, use its write() method."""
I think the documentation is trying to say: "don't mix high-level and low-level IO".
For most purposes, we can usually avoid using the low-level IO functions os.open() and os.write(). If we're using the low-level file functions because of pipes, then we can actually turn pipes into file-like objects by using os.fdopen(). os.fdopen() is a bridge that transforms file descriptors into file-like objects. See:
for more information on os.fdopen().
I hope this helps!