Python redirect stdout to file

Python 101: Redirecting stdout

Join the DZone community and get the full member experience.

redirecting stdout is something most developers will need to do at some point or other. it can be useful to redirect stdout to a file or to a file-like object. i have also redirected stdout to a text control in some of my desktop gui projects. in this article, we will look at the following:

  • redirecting stdout to a file (simple)
  • the shell redirection method
  • redirecting stdout using a custom context manager
  • python 3’s contextlib.redirect_stdout()
  • redirect stdout to a wxpython text control

redirecting stdout

the easiest way to redirect stdout in python is to just assign it an open file object. let’s take a look at a simple example:

import sys def redirect_to_file(text): original = sys.stdout sys.stdout = open('/path/to/redirect.txt', 'w') print('this is your redirected text:') print(text) sys.stdout = original print('this string goes to stdout, not the file!') if __name__ == '__main__':redirecting stdout / stderr redirect_to_file('python rocks!')

here we just import python’s sys module and create a function that we can pass strings that we want to have redirected to a file. we save off a reference to sys.stdout so we can restore it at the end of the function. this can be useful if you intend to use stdout for other things. before you run this code, be sure to update the path to something that will work on your system. when you run it, you should see the following in your file:

this is your redirected text:
python rocks!

that last print statement will go to stdout, not the file.

shell redirection

shell redirection is also pretty common, especially in linux, although windows also works the same way in most cases. let’s create a silly example of a noisy function that we will call noisy.py:

# noisy.py def noisy(text): print('the noisy function prints a lot') print('here is the string you passed in:') print('*' * 40) print(text) print('*' * 40) print('thank you for calling me!') if __name__ == '__main__': noisy('this is a test of python!')

you will notice that we didn’t import the sys module this time around. the reason is that we don’t need it since we will be using shell redirection. to do shell redirection, open a terminal (or command prompt) and navigate to the folder where you saved the code above. then execute the following command:

python noisy.py > redirected.txt

the greater than character (i.e. >) tells your operating system to redirect stdout to the filename you specified. at this point, you should have a file named “redirected.txt” in the same folder as your python script. if you open it up, the file should have the following contents:

Читайте также:  Minecraft launcher lib python

the noisy function prints a lot
here is the string you passed in:
****************************************
this is a test of python!
****************************************
thank you for calling me!

now wasn’t that pretty cool?

redirect stdout with a context manager

another fun way to redirect stdout is by using a context manager. let’s create a custom context manager that accepts a file object to redirect stdout to:

import sys from contextlib import contextmanager @contextmanager def custom_redirection(fileobj): old = sys.stdout sys.stdout = fileobj try: yield fileobj finally: sys.stdout = old if __name__ == '__main__': with open('/path/to/custom_redir.txt', 'w') as out: with custom_redirection(out): print('this text is redirected to file') print('so is this string') print('this text is printed to stdout')

when you run this code, it will write out two lines of text to your file and one to stdout. as usual, we reset stdout at the end of the function.

using contextlib.redirect_stdout

python 3.4 added the redirect_stdout function to their contextlib module. let’s try using that to create a context manager to redirect stdout:

import sys from contextlib import redirect_stdout def redirected(text, path): with open(path, 'w') as out: with redirect_stdout(out): print('here is the string you passed in:') print('*' * 40) print(text) print('*' * 40) if __name__ == '__main__': path = '/path/to/red.txt' text = 'my test to redirect' redirected(text, path)

this code is a little simpler because the built-in function does all the yielding and resetting of stdout automatically for you. otherwise, it works in pretty much the same way as our custom context manager.

redirecting stdout in wxpython

wxredirect

i have written about redirecting stdout in wxpython on several occasions. the following code is actually from an article i wrote in 2009 and updated in 2015:

import sys import wx class myform(wx.frame): def __init__(self): wx.frame.__init__(self, none, title="wxpython redirect tutorial") # add a panel so it looks the correct on all platforms panel = wx.panel(self, wx.id_any) style = wx.te_multiline|wx.te_readonly|wx.hscroll log = wx.textctrl(panel, wx.id_any, size=(300,100), style=style) btn = wx.button(panel, wx.id_any, 'push me!') self.bind(wx.evt_button, self.onbutton, btn) # add widgets to a sizer sizer = wx.boxsizer(wx.vertical) sizer.add(log, 1, wx.all|wx.expand, 5) sizer.add(btn, 0, wx.all|wx.center, 5) panel.setsizer(sizer) # redirect text here sys.stdout = log def onbutton(self, event): print "you pressed the button!" # run the program if __name__ == "__main__": app = wx.app(false) frame = myform().show() app.mainloop()

this code just creates a simple frame with a panel that contains a multi-line text control and a button. whenever you press the button, it will print out some text to stdout, which we have redirected to the text control. give it a try to see how well it works!

wrapping up

now you know several different methods for redirecting stdout to files. some methods are better than others. personally, i thought it was cool that python 3 now has a context manager built in just for this purpose. speaking of which, python 3 also has a function for redirecting stderr. all of these examples can be modified slightly to support redirecting stderr or both stdout and stderr. the very last thing we touched on was redirecting stdout to a text control in wxpython. this can be really useful for debugging or for grabbing the output from a subprocess, although in the latter case you will need to print out the output to have it redirected correctly.

Читайте также:  Python флаги при запуске

Published at DZone with permission of Mike Driscoll , DZone MVB . See the original article here.

Opinions expressed by DZone contributors are their own.

Источник

Redirect Stdout to a File in Python

Python uses the following two file objects on the sys library as output streams:

  • sys.stdout – used for output of print() function and the prompt in input() function,
  • sys.stderr – is an output stream for Python’s prompts (like the help() function), errors, and exceptions.

This article will focus on redirecting sys.stdout into a file instead of printing the output on the console using the following methods.

  • Method 1: Redirecting print() function output by passing file argument,
  • Method 2: Modifying the sys.stdout
  • Method 3: Redirecting using contextlib.redirect_stdout() on Python 3.4 +
  • Method 4: Using custom context manager in contextlib for Python 3.3 and older, and
  • Method 5: Shell redirection.

Method 1: Redirecting output of print statement to a file

The print() function contains a file argument, which determines where to send output. If we want to send the output of a print statement to a file, we assign the argument a path to a writable object. For example,

The first two print outputs are sent to the code1.log file, and the last one is not redirected – it’s sent to the stdout.

Method 2: Modifying sys.stdout

The sys.stdout, by default, sends output to the console. If we want to send the output to a file instead, we need to assign sys.stdout a writable file path, as shown below (please read the comments).

Note: After modifying sys.stdout to write output to a file, you may need to reset its behavior by using the following line to go back to sending the output to the console (see the code above).

You can also use context manager to modify sys.stdout behavior.

Method 3: Redirecting using contextlib.redirect_stdout() on Python 3.4 +

The contextlib library contains a redirect_stdout() that comes in handy when redirecting Python output. The function was added in Python 3.4. Here is an example of how to use it to redirect output to a file.

Method 4: Using custom context manager in contextlib for Python 3.3 and older

The redirect_stdout() function discussed in Method 3 is not available in Python 3.3 and older. The alternative is to use the contextmanager decorator within the contextlib library, as shown below.

Method 5: Shell redirection

This method involves executing Python script within the command line and redirecting the output to a file using the “>” or “>>” operators.

The “>” overwrites a file, whereas “>>” appends the output to a file without overwriting the file (if it existed).

The advantage of this method is that unlike the other four methods discussed above, shell redirection does not require us to modify the code to achieve the purpose.

As an example, save the following code to the code23.py script

File: code23.py

Then head to the terminal shell and execute the script above with the command:

Читайте также:  Java fields of superclass

That will redirect the output of the script to the code3.log file. If code3.log exists at the time of execution, the file is overwritten.

Alternatively, you can use the “>>” operator to send the output to a file.

If code4.log already exists, the script’s output is appended to the end of the file without overriding the original content.

Conclusion

This article discussed five ways of redirecting the output of a Python script to a file. The first method covered how to redirect the output of a print statement to a file, and method 2 discussed how to modify sys.stdout to enable output redirection to a file.

Methods 3 and 4 explained using the contextlib library to redirect output for different versions of Python.

The last method explained how to redirect the output of a Python script during the execution from the shell.

Источник

How to Redirect Stdout & Stderr to File in Python

redirect stdout stderr to file in python

Stdout is used by python to display text output of the commands that you run on your terminal or within a python script. Stderr is invoked when there is any error while running your command. This is also the case whenever you run any other command, not just python, and any other script such as shell script, in terminal. The problem is once these stdout and stderr messages are displayed they cannot be retrieved back later for future reference. The subsequent commands/scripts will push them out from display. In such cases, it is advisable to redirect stdout & stderr to file in Python. In this article, we will learn how to do this when you use python.

How to Redirect Stdout & Stderr to File in Python

In python, sys.stdout contains latest output of stdout and sys.stderr contains stderr output.

One of the ways to redirect these outputs to file is by setting them to a file object as shown below. Use this method if you want to redirect output from within script.

import sys with open('file', 'w') as sys.stdout: print('test')

Alternatively, you can redirect the output to a file(e.g. op.txt) using shell redirection.

Similarly, you can also set stderr to file as shown below. If it doesn’t work for you, please try the next method mentioned below.

import sys with open('file', 'w') as sys.stderr: print('test')

If you are using python>3.4, you can use contextlib.redirect_stderr to redirect stderr outputs to file. Here is an example.

from contextlib import redirect_stderr with open('filename.log', 'w') as stderr, redirect_stderr(stderr): print(1/0) # errors from here are logged to the file.

In the above example, we have deliberately raised an exception by dividing 1 by 0. The error message is written to filename.log file.

Unlike redirecting stdout to file, it is a little tricky to send stderr output to file since the error occurrence generally breaks code execution. So it is advisable to use contextlib for such purposes.

In this article, we have learnt how to redirect stdout and stderr to file. You can use them capture output and error messages of your python commands and scripts for future reference.

Источник

Оцените статью