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

Unlock This Lesson

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

Unlock This Lesson

Working With Multiline Files

00:00 In this lesson, which continues where you left off previously, you’ll learn a few ways of using Python to work with multiline text files. You’re free to use a differently named variable, but in this case, you reuse an existing variable by binding it to a new file object.

00:17 Now you can call .readline() to obtain the first line from the file. Notice that the return line includes a \n special sequence, which represents the universal newline character. This is a blank line, which has no content other than that line break. When you call .readline() again, then you’ll get the next line from the file.

00:39 You can keep calling .readline() manually, or you can conveniently loop through the file using a for loop.

00:52 The loop will pick up where you left off, and it’ll continue reading the subsequent lines from the file. The extra vertical space between the printed lines is the result of the fact that each line comes with a newline character of its own, while the print() function also appends a newline by default.

01:10 There are two ways to fix this. You can either strip any whitespace characters from the right end of each line before printing it …

01:29 or you can tell the print() function not to include the extra newline character by overriding the value of its end parameter.

01:47 Both calling .readline() and looping over the file line by line are great ways of processing very large files that would be too time- or resource-intensive to load as a whole into memory.

01:59 However, if you don’t mind loading the entire file into memory, then another approach you can use is to call the .readlines() method. That’s plural, .readlines(), which you shouldn’t confuse with the .readline() that you’ve already used.

02:16 The .readlines() method always returns a list, which can potentially be empty if there are no more lines in the file. But when you rewind to the beginning of the file, then it’ll return a list of strings corresponding to the lines in your file.

02:34 Just like the .read() method has a corresponding .write() method in the file object, .readlines() has a .writelines() counterpart, which takes a list of lines ending with a newline.

02:45 Let’s close the current file and reopen the other one in read-and-write mode.

03:03 The file is open at the beginning, like in the read-only mode, but it doesn’t overwrite the existing content, so you can read it. In this case, there’s only one line, which isn’t actually terminated with a newline character.

03:21 Because you opened the file in read-and-write mode, you can call any method on the corresponding file object, including the mentioned .writelines().

03:32 You need to pass a list with the lines to write—for example, a blank line followed by "the 3rd line" with a newline, "the 4th line" without a newline, and a fifth line.

03:49 Let’s rewind and read the file again.

03:55 Note that the fourth and fifth lines ended up mushed together instead of becoming two separate lines because you haven’t terminated the fourth line with an explicit line feed.

04:06 Even though you passed the lines as a list of elements, you should still terminate them with the universal newline character or \n special sequence.

04:15 Another convenient way of writing text to a file in Python is to call the print() function. By default, this function prints to the console, but you can optionally redirect it so that it will write to a specified file instead. To do so, you need to pass an extra parameter called file with a file object as the value.

04:43 When you call print() like that, you won’t have to remember to add the newline character yourself. Let’s take a peek at what you’ve just written in the file.

04:59 Great! That’s all you really need to know about reading and writing text files in Python. Let’s recap this knowledge before moving on.

Become a Member to join the conversation.