Redirigir salida estandar

David Sotelo aktin0s en gmx.net
Mie Jun 4 17:05:31 CEST 2003


On Wed, 04.06.2003 16:32 (+0200), Diego Chaparro wrote:

> El problema es que mi programa no acaba nunca, y necesito ir viendo lo que
> va escribiendo.
> 
> Imagino que será por el mismo motivo, mi programa no acaba nunca y por eso
> no escribe en el fichero. ¿Hay alguna forma de que vaya escribiendo en el
> fichero continuamente?

Puedes invocar al intérprete de python con la opción -u o usar el tercer
parámetro de la función open( ). Un ejemplo:

import sys, time

sys.stdout = open( 'fichero.log', 'a', 0 )
for i in range( 20 ):
    print i
    time.sleep( 1 )
sys.stdout.close( )

El 0 significa sin buffer.

The optional bufsize argument specifies the file's desired buffer size: 0
means unbuffered, 1 means line buffered, any other positive value means use
a buffer of (approximately) that size. A negative bufsize means to use the
system default, which is usually line buffered for for tty devices and fully
buffered for other files. If omitted, the system default is used.

Saludos.




Más información sobre la lista de distribución Python-es