Table of Contents
We use the print()
function in python to print any text to standard output. In this article, we will see how we can use the print()
function to directly print an output to a file. For this, we will first discuss the print()
function and then we will see how we can print to file in python using the print()
function.
The print() function in python
Normally, we use the print()
function to print different objects by passing one or more objects to the function as input argument as follows.
1 2 3 |
print("Java2blog") |
Output:
1 2 3 |
Java2blog |
Apart from the objects to print, the print()
function has five other parameters. To learn about them, let us look at the syntax of the print()
function.
print(objects, sep=separator, end=end, file=file, flush=flush)
Here,
- The
objects
are the objects that we normally pass to theprint()
function to print. - The
sep
parameter takes an input argumentseparator
. It is an optional parameter and theseparator
has a default value of whitespace“ ”
. This is why theprint()
function prints all the objects with whitespace between them. - The
end
parameter takes an input argumentend
to specify what should it print after printing all the objects. It is an optional parameter and the argumentend
has a default value“\n”
i.e. newline. - The
file
parameter is used to print the values of objects to an output stream. Here, the object passed in as thefile
argument must have awrite()
method. The parameterfile
is also an optional parameter and the input argumentfile
has a default valuesys.stdout
. This is why theprint()
function writes the values in the objects to the standard output by default. - The
flush
parameter is used to tell theprint()
function if the output is flushed (True) or buffered (False). It is also an optional parameter and theflush
argument has a default valueFalse
.
How to Print to file in python?
To print to file in python, we will have to direct the print()
function to print the output to the file using the file
parameter discussed above. We can also redirect the standard output stream to the file object by using the sys
module. Let us discuss both these approaches one by one.
Print to file using the file parameter in the print function
To print the output of the print()
function to a file, we will first open a file in write mode using the open()
function. The open()
function takes a file name
and mode
as input arguments and returns a file object after successfully opening the file. Here, we will pass the file name as the first input argument to the open()
function. We will use the file in write mode by using “w”
as the second input argument to the open()
function.
You should keep in mind that the open()
function will erase the contents of any existing file with the same file name after opening it when we use the write mode. If there exists no file with the given file name, the open()
function will create a new empty file.
After opening the file, we will pass the file object as an input argument to the file parameter of the print()
function. In this way, the output of the print()
function will be written to the file.
After writing to the file, Don’t forget to close the file using the close()
method. Otherwise, the changes will not be saved.
The python code for this entire process is given below.
1 2 3 4 5 |
myFile = open("sample.txt", "w") print("This will be written to the file instead of the output screen", file=myFile) myFile.close() |
After printing contents to the file, you can open the file in the read mode to verify the contents of the file as shown below.
1 2 3 4 5 6 7 8 9 |
myFile = open("sample.txt", "w") print("This will be written to the file instead of the output screen", file=myFile) myFile.close() myFile = open("sample.txt", "r") text= myFile.read() print(text) myFile.close() |
Output:
1 2 3 |
This will be written to the file instead of the output screen |
Further reading:
Print to file by redirecting standard output
Instead of using the file
parameter, we can use the sys
module to divert the standard output to the file. For this, We will use the following steps.
- First, we will open the file using the
open()
function. Theopen()
function returns a file object that we will keep in a variable namedmyFile
. - After that, we will assign the file object
myFile
to the standard output streamsys.stdout
. - After this, whenever we will use the
print()
statement, the function will print the output to the file.
You should keep in mind that sys.stdout
is a default environment variable. So, we should not change its value. Therefore, we will store the value of sys.stdout
in a variable, and after printing contents to the file, we will assign the default value to sys.stdout
. After assigning the default value to sys.stdout
, you should also close the file using the close()
method. Otherwise, the contents written in the file will not be saved.
The following python code prints contents to a file by redirecting the standard output.
1 2 3 4 5 6 7 8 9 10 |
import sys default_value = sys.stdout myFile = open("sample.txt", "w") sys.stdout = myFile print("This will be written to the file instead of the output screen.") sys.stdout = default_value myFile.close() |
After closing the file, you can open the file in the read mode to verify the contents of the file as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import sys default_value = sys.stdout myFile = open("sample.txt", "w") sys.stdout = myFile print("This will be written to the file instead of the output screen.") sys.stdout = default_value myFile.close() myFile = open("sample.txt", "r") text = myFile.read() print(text) myFile.close() |
Output:
1 2 3 |
This will be written to the file instead of the output screen. |
Conclusion
In this article, we have discussed the syntax of the print()
statement. After that, we have discussed two ways to print contents directly to a file using the print()
function. I would suggest you use the approach with the print()
function and the file
parameter as it doesn’t require you to change the environment variables. If you will use the second approach by changing the environment variables, you might forget to revert back the changes and it may cause errors in your programs.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!