confusion about opening files

Alex Martelli aleax at
Tue Sep 24 03:40:05 EDT 2002

Arief wrote:

[ I'm putting last things first here...:-) ]
> I hope it would help you. Any correction is welcomed ...

No corrections -- just a few extra tidbits...

> The is os syscall (system call). It is used to open file, fifo,
> device, etc in UNIX or Linux environment (I don't know about MS Windows).

On Windows, Visual C++'s runtime libraries simulate the Unix/Linux/BSD
system calls open / read / write / etc to some extent, basically to
ease porting programs originally written for Unix &c.

> The built in function: "open" uses c function "fopen". It is a buffered
> version of os syscall "open" implementation. You should use appropriate

You can, if you wish, open a file in unbuffered mode, too.

> function for further operation too, for example:
> fname = '/tmp/a.txt'
> f = open(fname, 'w')

f = open(fname, 'w', 0)

gives you an unbuffered file object f.

> It is "not guaranteed" that as soon as you complete the f.write call, the
> file is written with data you've feed. Because it is a buffered operation.
> So to assure the OS to write your text immediately, you should do this:
> fname = '/tmp/a.txt'
> f = open(fname, 'w')
> f.write('this is only example\n')
> f.flush()             # flush buffered data for previous write operation
> f.close()

No need to flush right before closing, ever -- close flushes all that
needs to be flushed.

No need to flush if you open the file in unbuffered mode.  With unbuffered
files, or frequent flushing, I/O performance may degrade.

Flush, and unbuffered files, ASK the OS to please, please write the
data out -- but not all OS's comply.  A traditional problem with DOS
and related systems (I'm pretty sure it still was in Windows/98) is
that the OS _doesn't_ comply with your kind request: if your program
crashes without closing files, the files may end up empty even though
you wrote and flushed repeatedly.  The only "cure" (HA!) to this
horrid situation was to *CLOSE AND REOPEN FOR APPEND* such files
periodically (talk about performance being degraded...!!!).  I hope
(but don't know for a fact) that there are no such horrid issues
with current OS'es such as Win/XP.


More information about the Python-list mailing list