Redirect stdout & stderr (similar to a daemon)

Tsai Li Ming mailinglist at ltsai.com
Sun May 30 20:19:23 EDT 2004


Donn Cave wrote:
> 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

Thanks Donn,

Why is there a need to close old0? Because of the extra file handle?

Liming



More information about the Python-list mailing list