The print() function of Python is one of the most widely used functions. Developers mostly use it to display strings and variable values each in the interactive interpreter or the compiler. It entirely depends on what the Python program is running.
However, there's a possibility to change its operation from writing to console to writing text to a file. This article will talk about how to write to a Python file through the print() function.
Method 1: Redirecting the standard output data to a File:
Python's print() function is more flexible than you can imagine. There are various ways and operations that programmers can perform using print(). Apart from printing data to the console, it also helps in sending texts to a location called the standard output stream (stdout). The other two streaming pipes are stdin and stderr.
By default, the standard output (stdout) pipe aims at the interactive window that helps in program execution. We can also redirect the standard output to other locations, such as text files. That is where we can use the print() function with different approaches or parameters.
import sys print('Displaying this message on the output console.') orig_stdout = sys.stdout with open('dataFile.txt', 'w') as gr: # Changing the standard output sys.stdout = gr print('Writing the codename GR26 to the newly created file.') sys.stdout = orig_stdout
First, we will import the sys module. Then, we will print a message that gets displayed on the console output. Then we create a variable orig_stdout where we initialized the value of sys.stdout. Then, we have to open a file (dataFile) with the write mode and alias it as ‘gr’. Next, within its body, we will assign the file to the standard output.
Finally, whatever string we will pass in the print() function will get written in the file from the standard output. The sys.stdout = orig_stdout will reset the standard output to its original value.
Method 2: Redirecting through Standard Error Stream:
When any error encounters in Python, it is written to the standard error stream (stderr). To print the value of stderr in the file, we need to redirect it. For this, we could simply redirect the sys.stdout to point to the sys.stderr, through the file parameter of the print() function.
It is beneficial when debugging small programs. However, it is recommended to better use a debugger in case of large programs.
import sys print('Displaying this message on the output console.') orig_stdout = sys.stdout sys.stdout = sys.stderr print('Writing the codename GR26 to the newly created file.') with open('dataFil.txt', 'w') as gr: sys.stderr = gr print(sys.stderr, file = sys.stderr) sys.stdout = orig_stdout
First, we will import the sys module. Then, we will print a message that gets displayed on the console output. Then we create a variable orig_stdout where we initialized the value of sys.stdout. We will then assign the sys.stderr to sys.stdout. Here we are calling the print() that will display some strings but this string won’t get written to the file. Instead we will write any standard error to the file.
Then, we have to open a file (dataFil) with the write mode and alias it as ‘gr’. Next, within its body, we will assign the file to the standard error pipe. Then we will use the print() and put 2 parameters; print(sys.stderr, file = sys.stderr).
Finally, we assign sys.stdout = orig_stdout to reset the standard output to its original value.
Method 3: Printing specific value to a File:
Instead of writing any standard pipe value, programmers can also write specific values to a file using the print(0) function. If the file does not exist, this process creates a new file by that name and write the string values passed to the print().
sampl = open('AnyFile.txt', 'w') print('Karlos, Dee, Sue, Bill, Steve, Elon', file=sampl) sampl.close()
This program is a simpler one and does not require the sys module. Here we will open a new file (AnyFile) in write mode and assign it to a file object (sampl). Then, we will print few values through the print() function and apply a parameter ‘file’ that will take the file into which it will write the strings. Finally, we do close the file object.
All these three techniques are useful and allows Python programmers to write to a file using the print() function. But using the with open() and the aliasing (in the first two examples) increases the program complexity.
Thus, it is always better to use the third method as it is more efficient and less time taking. But many developers and researchers need to display the error or the standard output directly to the file. In that case, the third method is not at all useful and thus programmers and developers need to go with the first two approaches.