Alex Flint
Alex Flint

Reputation: 6686

Redirect output at libc level

I want to redirect output in such a way that works for output generated by C modules that print directly to stdout. The standard approach doesn't work here:

sys.stdout = open('out.txt', 'w')

because this doesn't operate at the libc level.

The function freopen() can do this, but how can I access this from python?

Upvotes: 0

Views: 86

Answers (1)

glglgl
glglgl

Reputation: 91017

Open the file with os.open and the appropriate options (os.O_WRONLY etc.) and afterwards do a os.dup2(<new fd>, 1).

Upvotes: 2

Related Questions