The open() function opens a file. You must use the “r” mode to read a file. The read(), readline(), readlines() functions return the contents of a file you have opened.
Python is a useful programming language to use if you want to process data. The language has several built-in functions that make it easy to read, write, and manipulate data or files.
Let’s say that you have a list of employee names, and you want to check if a user’s name is on that list. You could save the list of employee names as a file. Then, you could use Python to read that file and check if the employee’s name is stored within that file.
In this tutorial, we are going to explore the basics of reading files in Python. To start, we will discuss how to open and access a file. Then we will go on to explore how to read a file in Python.
Open File for Reading in Python
The first step to reading a file in Python is to open the file you want to read. You need to tell Python the name of the file you want to open.
To read a file, you must first tell Python where that file resides. You can do so by specifying the path of the file and declaring it within a variable.
Here’s the syntax for opening a file in Python:
filename = "/users/career_karma/names.txt" names_file = open(filename, 'r')
Our code opens a file at the path we defined in the “filename” variable.
The r flag at the end of the open() function tells Python that we only want to read our file. We could change this flag if we wanted to edit our file.
Python Read File
Now that our file is open, we can read it through Python. There are three functions that we can use to read data from a file, which are as follows:
- read(): Returns the contents of a file
- readline(): Returns the next line of a file
- readlines(): Returns a list of lines in a file
Let’s break down how each of these works. The read() method can return the entire contents of a file as a string.
Python Read Text File
Here’s an example of read() operating on a text file that contains a list of names:
path = "/users/career_karma/names.txt" names_file = open(path, 'r') print(names_file.read())
Our program returns the following:
"Sally\nAlex/nPamela/nJonas/nLuke/nWill/n"
The read() method returned everything within our names file, including the newline characters at the end of our string.
The readline() function returns the next line of a file. readline() returns the text and the newline character at the end of the file. The following code will read the first line in our file:
print(names_file.readline())
Our code returns:
"Sally\n"
If we wanted to read the first and second lines of our existing file, we would need to use the readline() function again:
names_file = open(path, 'r') print(names_file.readline()) print(names_file.readline())
Our code returns the following output:
"Sally\n" "Alex\n"
Python Read File Line-by-Line
The readline() function can be useful if you want to read a file line by line. The function is commonly used within a for loop to read multiple lines in a file, like this:
for i in range(0, 2): print(names_file.readline())
Our code returns the following:
"Sally\n" "Alex\n"
Finally, the readlines() method returns a list of the lines in a file. This function returns an array, and each item represents a single line within a file:
print(names_file.readlines())
The program returns the following:
['Sally\n', 'Alex\n', 'Pamela\n', 'Jonas\n', 'Luke\n', 'Will']
Once you have read a file, you can’t reread it. So, when you have read the file using readlines(), trying to reread the file using any read file operation will return an empty string. If you want to read a file multiple times, you’ll need to open the file again and read it.
Conclusion
You can use the read(), readline(), and readlines() functions to read certain parts of a file. Then, you manipulate the data depending on your needs. Today, we covered how to open a file, read it using specific methods, and how to close it in Python.
Now you’re ready to open and read Python files like an expert! To learn more about Python, read our guide on How to Code in Python.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.