print fileinput, and screen ?

Yigal Duppen yduppen at xs4all.nl
Tue Jul 23 09:59:37 EDT 2002


> and as i do some other printing to stdout (print script times, counts,
> etc.) i'm wondering if that couldn't conflict with "print" for
> fileinput ?

If your stdout is redirected, you can always reach the original stdout 
file-like object with sys.__stdout__

Example:

### Redirecting stdout:
>>> import sys
>>> f = open("/tmp/example", "w")
>>> sys.stdout = f

### Printing to the redirected stdout:
>>> print "Hello World"
>>> sys.stdout.write("Hello World\n")

### Printing to the real stdout:
>>> sys.__stdout__.write("Hello World\n")
Hello World

### Resetting stdout
>>> sys.stdout = sys.__stdout__
>>> print "Hello World"
Hello World

## Closing f and inspecting it from the shell
>>> f.close()
>>> ^D
$ cat /tmp/example
Hello World
Hello World


However, since sys.__stdout__ is a file-like object, you MUST add all 
end-of-lines yourself, since print won't do that.

YDD
-- 
.sigmentation Fault



More information about the Python-list mailing list