Today we’ll learn how to send text to file using the print function.
Here’s the video version if you want to watch it first:
We usually use the print function to send text to the standard output stream, to which the sys.stdout object defaults.
But if you want, you can print text to a file instead. One way to do it is to manually reassign the sys.stdout object to a file and then, when done, reassign it back to the standard output stream. But then we have to first store the value of the standard output stream and then reassign back to it using the stored value. But there’s an easier way to do it.
You can use the print function with the file keyword argument to specify the exact destination for our string. Be sure to assign a file object to it. You can use various access modes, like ‘w’, ‘r’ or ‘a’.
Writing to File
Let’s start by writing to file. If you use the ‘w’, or write, access mode, the file is created if it doesn’t exist. Suppose we want to send the text ‘part 1’ to the ‘sample.txt’ file in the C:\code folder. The folder is now empty:
As the sample.txt file doesn’t exist yet, it will be created when we send text to it:
>>> print("part 1", file = open('C:\code\sample.txt', 'w'))
As you can see, we are not getting the output on the screen. Instead it’s saved in the sample.txt file. If you now open the code folder again, you will see the file in it:
And if you open the file, you will see the text that you sent to it:
Now, if the file you’re sending text to already exists, it gets overwritten, so only the last text sent to the file will survive. Run this two lines of code one by one:
>>> print("part 2", file = open('C:\code\sample.txt', 'w'))
>>> print("part 3", file = open('C:\code\sample.txt', 'w'))
If you now go to the code folder and open the sample.txt file, it will only contain the text that you sent as last:
Reading from File
So far we’ve been viewing the contents of the file by manually opening it. Now let’s read the contents of the file in code. The access mode for reading is ‘r’:
>>> print(open('C:\code\sample.txt', 'r').read())
part 3
As you can see, the text from the file has been read and printed. You don’t even need to use the ‘r’ access mode, because it’s the default one, so instead you could just type:
>>> print(open('C:\code\sample.txt').read())
part 3
Appending Text to File
If there is some text in the file and you don’t want to overwrite it, you can append new text to it. If you use the ‘a’, or append, access mode, new data will be appended at the end. As you know, our text now reads ‘part 3’. Let’s append two strings to it, ‘part 1’ and ‘part 2’:
>>> print("part 1", file = open('C:\code\sample.txt', 'a'))
>>> print("part 2", file = open('C:\code\sample.txt', 'a'))
You can now open the file manually:
or you can read its contents:
>>> print(open('C:\code\sample.txt').read())
part 3
part 1
part 2
This article is not specifically about file objects and access modes, so I’m not going to go into more detail here, but this is how you can use the file keyword argument in the print function to redirect the stream to a file.