Redirect Stdout to a File in Python

Redirect stdout to a file in Python?

If you want to do the redirection within the Python script, setting sys.stdout to a file object does the trick:

# for python3
import sys
with open(‘file’, ‘w’) as sys.stdout:
print('test')

A far more common method is to use shell redirection when executing (same on Windows and Linux):

$ python3 foo.py > file

How to redirect stdout to both a file and `sys.__stdout__`?

You can make your own file-like object whose write method sends the data to both places and install it as sys.stdout.

How to redirect 'print' output to a file?

The most obvious way to do this would be to print to a file object:

with open('out.txt', 'w') as f:
print('Filename:', filename, file=f) # Python 3.x
print >> f, 'Filename:', filename # Python 2.x

However, redirecting stdout also works for me. It is probably fine for a one-off script such as this:

import sys

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

for i in range(2):
print('i = ', i)

sys.stdout = orig_stdout
f.close()

Since Python 3.4 there's a simple context manager available to do this in the standard library:

from contextlib import redirect_stdout

with open('out.txt', 'w') as f:
with redirect_stdout(f):
print('data')

Redirecting externally from the shell itself is another option, and often preferable:

./script.py > out.txt

Other questions:

What is the first filename in your script? I don't see it initialized.

My first guess is that glob doesn't find any bamfiles, and therefore the for loop doesn't run. Check that the folder exists, and print out bamfiles in your script.

Also, use os.path.join and os.path.basename to manipulate paths and filenames.

Redirect the output of python script into a text file

Just use standard shell redirection:

python your_script.py > output_file.ext

If you want to write to a file directly from the script without redirecting:

with open("output.ext", "w") as stream:
print >>stream, "text"

Redirect stdout to a text file in Python?

Instead of using sys.stdout you can write like:

output_file = open(log_file, 'w')

output_file.write('testing asdasdfsd')

Or if you want to write all kinds of print value in log file then :

output_file = open(log_file, 'w')

sys.stdout = output_file

that's it.

Python, redirect some output to file from shell

The simplest solution is to make the stuff you want to go to the shell go to stderr, not stdout (the default target for print):

#!/usr/local/bin/python3.9

import sys

print("Writing to file...", file=sys.stderr) # prints to console because stderr not redirected
print({"test": 1}) # writes to output.json because stdout is redirected

The user could independently redirect stderr if they like, but the example command would leave it going to the terminal. This is typically how programs split up "debug output" from "productive output" when not explicitly writing "productive output" to a specific file.

Can I redirect the stdout into some sort of string buffer?

from cStringIO import StringIO # Python3 use: from io import StringIO
import sys

old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()

# blah blah lots of code ...

sys.stdout = old_stdout

# examine mystdout.getvalue()


Related Topics



Leave a reply



Submit