Skip to main content
Import O_WRONLY from os
Source Link
Thomas
  • 10.8k
  • 4
  • 30
  • 35

The other answers didn't cover the case where you want forked processes to share your new stdout.

To do that:

from os import open, close, dup, O_WRONLY old = dup(1) close(1) open("file", O_WRONLY) # should open on 1 ..... do stuff and then restore close(1) dup(old) # should dup to 1 close(old) # get rid of left overs 

The other answers didn't cover the case where you want forked processes to share your new stdout.

To do that:

from os import open, close, dup old = dup(1) close(1) open("file", O_WRONLY) # should open on 1 ..... do stuff and then restore close(1) dup(old) # should dup to 1 close(old) # get rid of left overs 

The other answers didn't cover the case where you want forked processes to share your new stdout.

To do that:

from os import open, close, dup, O_WRONLY old = dup(1) close(1) open("file", O_WRONLY) # should open on 1 ..... do stuff and then restore close(1) dup(old) # should dup to 1 close(old) # get rid of left overs 
added 5 characters in body
Source Link
Yam Marcovic
  • 8.2k
  • 1
  • 31
  • 39

The other answers didn't cover the case where you want forked processes to share your new stdout.

To do that:

from os import open, close, dup old = dup(1) close(1) open("file", 'w'O_WRONLY) # should open on 1 ..... do stuff and then restore close(1) dup(old) # should dup to 1 close(old) # get rid of left overs 

The other answers didn't cover the case where you want forked processes to share your new stdout.

To do that:

from os import open, close, dup old = dup(1) close(1) open("file", 'w') # should open on 1 ..... do stuff and then restore close(1) dup(old) # should dup to 1 close(old) # get rid of left overs 

The other answers didn't cover the case where you want forked processes to share your new stdout.

To do that:

from os import open, close, dup old = dup(1) close(1) open("file", O_WRONLY) # should open on 1 ..... do stuff and then restore close(1) dup(old) # should dup to 1 close(old) # get rid of left overs 
Source Link
Yam Marcovic
  • 8.2k
  • 1
  • 31
  • 39

The other answers didn't cover the case where you want forked processes to share your new stdout.

To do that:

from os import open, close, dup old = dup(1) close(1) open("file", 'w') # should open on 1 ..... do stuff and then restore close(1) dup(old) # should dup to 1 close(old) # get rid of left overs