Skip to content
Home » How to Send Text to File Using the print Function

How to Send Text to File Using the print Function

Spread the love

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:

empty folder

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'))

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

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:

sending text to file

And if you open the file, you will see the text that you sent to it:

file contents

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

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:

overwriting a file

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

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

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:

appending to file

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.


Spread the love

Leave a Reply