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 for Beginners –Cheat Sheet Data types and Collections i nt eger 10. String Methods s. S t rip remove trailing whitespace s. S p l it(x) return list, delimiter x s. J o in (l) return string, delimiter s s. S t artswith (x) return True if s starts with x. Python Strings cheat sheet of all shortcuts and commands.
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/keyword | parameters | uses |
---|---|---|
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 string | write the string to the file that is opened with a certain writeable mode |
writelines() | the list of strings | write the list of strings to the file that is opened with a certain writeable mode |
close() | - | close the opened file |
mode | read | write | create | truncate | cursor position |
---|---|---|---|---|---|
r | Yes | start | |||
w | Yes | Yes | Yes | start | |
a | Yes | Yes | end | ||
r+ | Yes | Yes | start | ||
w+ | Yes | Yes | Yes | Yes | start |
a+ | Yes | Yes | Yes | end | |
x | Yes | start |
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.
Python Command Cheat Sheet
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()
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.
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.
Close a File
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 String Cheat Sheet Pdf
Python Functions 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.