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

Recapping Text File Operations

00:00 In this lesson, you’ll get a quick summary of what you’ve just learned about reading and writing text files in Python. When you open a file in Python in a mode that enables reading, such as the read-only mode denoted with the letter code r, you can read the file in the following few ways. First, you can load the entire file’s contents at once into a Python string by calling the file object’s .read() method. However, this is only recommended for smaller files.

00:30 You can optionally request the upper limit of characters to read at a time by providing the maximum number as a parameter to the .read() method.

00:40 Alternatively, you can read the next line until reaching a line ending in the file. Note that this will include the trailing newline character, which you can remove by calling the string’s .rstrip() method.

00:55 You can also read all lines at once and put them into a Python list, which may sometimes be empty if there are no more lines in the file. Finally, you can iterate over the file object line by line, which might be useful if the file doesn’t have a known size or is too big to fit in the memory. This is similar to manually calling the .readline() method in a loop.

01:18 Note, however, that each line will contain a trailing newline, which you could strip as before, or you can tell the print() function not to append the extra newline character at the end.

01:31 When you open the file in a mode that allows for writing new content to it, then you also have a few choices. Specifically, you can write a piece of text to the file by calling the .write() method with a Python string as an argument. However, you have to remember to append the \n special sequence at the end of the string to terminate the line.

01:56 You can also write more than one line at a time by passing them as a list of strings to the .writelines() method.

02:06 Finally, you can also call the print() function with an optional file argument bound to the file object you want to write to. In this case, the extra newline character appended by each call to the print() function is quite convenient.

02:22 Reading and writing text files in Python isn’t terribly difficult once you master the basics. However, when you start dealing with actual files in real life, then you’ll quickly notice that it can get a bit tedious. Therefore, at some point, you might consider using a higher-level abstraction that hides the implementation details of reading and writing text files formatted in some specific way, like CSV, or comma-separated values. In the next lesson, you’ll use Python’s built-in csv module to read tabular data, which you could have exported from a database or a spreadsheet program, for example.

Become a Member to join the conversation.