O_APPEND & O_WRONLY

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Tue Dec 11 08:42:44 EST 2001


----- Original Message ----- 
From: "waalp" <waalp at pissed.co.uk>


> I'm trying to open a file in append mode (using python under ms windows)
> if i use the O_APPEND parameter i get a errormessage from the interpreter
> like this:
> OSError: [Errno 9] Bad file descriptor
> 
> But if i use the O_WRONLY parameter everything goes write.
> 
> But i want to append the file, not overwrite it?
> 
> am i doing something wrong here?

Yup, I'm afraid so... see my notes below:

> my code:

> #!/usr/bin/python
> from os import *

This is a real bad idea.  Just 'import os' and add os. to the beginning
of everything... you are overriding builtins with incompatible interfaces.

> def writeLog(logMSG):
>     logfd = open('event.log',O_APPEND)

You need to bitwise-OR the O_APPEND and O_WRONLY flags (AFAIK).

>     write(logfd, logMSG)
>     close(logfd)
> 
> writeLog('hello world')

Why aren't you using the normal Python builtin file functions?

I'd write your function like this:

##################################

def writeLog(logMSG):
    log = open("event.log", "a")
    log.write(logMSG)
    log.close()

writeLog('hello world')

##################################

Or even better:

##################################

log = None

def writeLog(logMSG):
    global log
    if log is None:
        log = open("event.log", "a")
    log.write(logMSG)
    log.flush()

writeLog('hello world')

##################################







More information about the Python-list mailing list