Redirect stdout & stderr (similar to a daemon)

Donn Cave donn at drizzle.com
Sun May 30 12:33:52 EDT 2004


Quoth mwilson at the-wire.com (Mel Wilson):
| In article <40b9cd69 at news.starhub.net.sg>,
| Tsai Li Ming <mailinglist at ltsai.com> wrote:
...
|> # Redirect standard file descriptors.
|> os.dup2(si.fileno(), sys.stdin.fileno())
|> os.dup2(so.fileno(), sys.stdout.fileno())
|> os.dup2(se.fileno(), sys.stderr.fileno())
|
|    These modify the actual file objects represented by
| 'sys.stdin', etc.  There's no coming back from that.

There is, if I understand what you meant by that.  Consider

   old0 = os.dup(0)
   os.dup2(si.fileno(), 0)

The original input stream is held open on some arbitrary
unit, which you may use at any point to restore it to standard
input.

   os.dup2(old0, 0)
   os.close(old0)

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list