Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Writing Text to a File

00:00 In this lesson, you’ll get a hands-on introduction to reading and writing text files in Python, so go ahead and jump into IDLE. First, you’re going to need some sample text to work with.

00:12 You can obtain this text by importing the standard-library module called this. As you can see, it prints out the so-called Zen of Python, which is a humorous poem expressing the principles of the language.

00:26 You can grab that and assign it to a variable. Let’s call the variable text. You need the triple-quote to start a multiline string. Otherwise, you’ll get a SyntaxError.

00:40 The text contains some newline characters noted with \n.

00:46 Now, there are several ways in which you can write this text to a file. First of all, you need to open the file in write-only mode, remembering to specify the character encoding.

01:05 In real-life projects, you should generally prefer using the pathlib module to open a file in Python because you’ll get improved portability. However, calling the built-in open() function here in IDLE will be more convenient for the sake of explaining things. Even though the Zen of Python is an English text, you should make it a habit to use UTF-8 whenever possible, which is the best way to ensure that your text will be easily read and understood by any computer or text editor.

01:34 The letter code w opens the file in write-only mode, which means that Python will create the file for you or erase its contents if the file already exists. So remember to be careful with this mode.

01:47 Also, when you open the file in the write-only mode by setting the mode argument to the letter code w, then you won’t be able to call any of the reading methods on the associated file object. As soon as you call file.read(), you’re getting an UnsupportedOperation error, which means that method doesn’t work in files opened in the write-only mode.

02:09 One of the operations that is supported in this mode, though, is the .write() method. You can call this method with a string as an argument to write a whole text in one go.

02:25 The value returned by .write() is the number of characters written to the file. In this case, it’s exactly the same as the length of your text.

02:36 Note that in binary mode, the .write() method returns the number of actual bytes written to the file instead of the number of characters, which might be slightly different.

02:46 Calling .write() with the entire text is only suitable for reasonably short texts that can fit in your computer’s memory. If your text is longer or isn’t known up front, then you can write the text file incrementally by calling the .write() method multiple times in your with code block. Here’s an example.

03:04 Let’s open another file called incremental.txt, also in .write() mode and with the UTF-8 encoding.

03:17 You can keep calling .write() more than once with a different arguments until you leave the with code block.

03:32 This time, the first call to .write() returns seven characters and the second one six characters. You can confirm the contents of the file you’ve just created by reading it back using Python. Now that you know how to write text into a file, it’s time to implement code that will retrieve the text from that file.

Become a Member to join the conversation.