This short tutorial isn’t intended to cover everything in file reading and writing in Python. Instead, I just wanted to share with you some cheat sheets that I created for reading and writing files in my daily Python programming.

Python 3 Beginner's Reference Cheat Sheet Special characters # comentand n new lineor scape char dict.get Numeric operators + addition - subtraction. multiplication / division. exponent% modulus // floor division Boolean operators equal!= different higher = higher or equal String methods string.upper converts to uppercase. In Python, there are a lot of things you can do with strings. In this cheat sheet, you’ll find the most common string operations and string methods. Return True if string starts with the prefix. A truly Pythonic cheat sheet about Python programming language. Exhaustive, simple, beautiful and concise. A truly Pythonic cheat sheet about Python programming language. Also works on strings. = methods the class has implemented. For instance, Iterable ABC looks for method iter while Collection ABC.

  • The code presented here is written in Python 3.7.3, and doesn’t always apply to earlier versions of Python.

  • We’ll focus on built-in methods that come as part of the standard package so that we don’t need to deal with installing third-party packages that are not necessarily straightforward for beginners.

Python File Operations CheatSheet

Here are the two quick cheat sheets as the takeaways for this article. The first sheet lists the key methods/functionalities involved in reading and writing files that are discussed in this article.

method/keywordparametersuses
open()filename and open mode (optional)create a file object by opening/creating the file in the specified read/write mode
with-use it together with open(); closes the file after the suite completes
read()size (optional)read the file up to the size specified if set
readline()size (optional)read a single line with a size limit if set
readlines()size (optional)create a list of the read lines with an optional size hint
for loop-iterate the lines of the file
write()the stringwrite the string to the file that is opened with a certain writeable mode
writelines()the list of stringswrite the list of strings to the file that is opened with a certain writeable mode
close()-close the opened file
modereadwritecreatetruncatecursor position
rYesstart
wYesYesYesstart
aYesYesend
r+YesYesstart
w+YesYesYesYesstart
a+YesYesYesend
xYesstart

Python files mode

Open a File

open()

The first step in dealing with a file in Python is to open the file. To do that, you can just use the open() method, which will create a file object. Specifically, this method has a set of parameters for reading a file, but the two most used arguments are the filename and the mode.

Suppose that we have the following text file named test.txt for the purpose of this tutorial. You can create one like the below in your Python working directory if you want to try these methods while following this tutorial.

To open this file, we can simply call the open() method like below. We specified the filename and the open mode. As you can see from the Terminal, a file object or a text stream (i.e. TextIOWrapper) is created.

The list above (i.e., cheat sheet 2) summarizes the common open modes with their intended uses. If the open mode is omitted, the default mode will be r.

To open the file in the binary mode, you need to append the letter b after the mode listed above (e.g., rb, rb+). The binary mode should be used when the file doesn’t contain text.

with keyword

We can use the with keyword when a file is opened.

Using the with keyword allows the file to be properly closed automatically after the suite completes, even when an exception is raised during the process.

In the above code snippet, a few things are worth noting.

  • When the mode argument is omitted, the text file is read in the text mode and the line endings are converted to n.

  • Using the with keyword, the file object is closed as expected

Read a File

read()

As you’re probably aware, we used the read() method just now. In this method, you can specify the size that you want to read from the file object.

When the size argument is omitted or set as negative, or set as an integer greater than the file size, all the file contents will be read.

We can also specify a size smaller than the file size. In the example below, by specifying the size to be 6, only the first 6 characters will be read for the text file.

If the end of the file has been reached after reading, an empty string (') will be returned.

For the example given below, an empty string is returned by the second read() method, as the first call has completed the reading of the entire file.

readline()

Python String Methods Cheat Sheet

We can also read individual lines by using the readline() method. This method will read the text for the entire line including the line ending (i.e., n).

However, the n will be omitted for the last line of the file, if the file doesn’t end in a newline. When reading has reached the end of the file, an empty string will be returned.

If you call the readline() method multiple times, the reading will be continued at where it was read last time.

Actually, the readline() method can also take in a size parameter, which will be used as a limit for reading the line.

In the code snippet below, readline(2) and readline(5) will read just 2 and 5 characters, respectively. Again, reading is continued at where it was read last time.

One more thing to note is that a blank line is returned as 'n' — a string containing only a single new line.

readlines()

If you want to read all the lines with a single method, you can use readlines(), which will create a list of individual lines.

The readlines() method can also optionally set a size hint.

When it’s set as a positive integer, it will read that many characters (or bytes in the binary mode) from the file and enough to complete that line.

In our case, our first line is 29 characters. Thus, a size of 1 to 29 will lead to the reading of the first line, while 30 or greater will read both lines, as shown below.

Write a File

write()

To write new values (e.g., text) to the file, we can use the write() method.

However, how the new values will be written to the file is dependent on the open mode as summarized in cheat sheet 2.

For example, the w mode will truncate the file, and thus the file will only contain the new values. The a mode will allow you to append new values to the existing file.

The above code writes the specified string to the file after truncating the text before it was opened.

A side note is that the write() method will automatically print the number of characters written, which can be suppressed by assigning the returned value to an underscore:

After the writing action, you can see the current text of the file. As expected, only the new string is contained in the file.

Python String Methods Cheat Sheet Answers

writelines()

We can also use the writelines() method to write multiple lines to the file.

This method will take in a list of strings as the parameter. How the lines are written (e.g., overwriting or appending) using this method is similar to the implementation of the write() method.

After executing the above code, the file’s text becomes 'Line 0nLine 1' as read by the default read() method.

Python Oop Cheat Sheet

Close a File

Python String Manipulation Cheat Sheet

After you’re done working with the file, it’s always good practice to close the file by calling the close() method on the file object.

Python Command Cheat Sheet

As shown below, before the file is closed, the closed method returns False, and it returns True after we call the close() method.