Reading text files is a fundamental skill in Python programming. Whether you are working with data analysis or automation, you will need to read the contents of a file. Manually reading each file can be time-consuming. Python makes this task simple, fast, versatile, and beginner-friendly, offering multiple ways to read and process files effectively. In this blog, we will cover everything you need to know about reading a text file in Python, including different methods, best practices, error handling, and practical examples to ensure you feel confident in handling files in your projects.
Table of Contents:
What Does Reading a File Mean in Python?
Reading a file in Python refers to accessing and getting the information from a file, so that your program can then process, analyse, or manipulate what the interpreter has read. A file can be a .txt file, a log file, or a configuration file. Typically, they are files containing information organised as lines of text. When you read that file, Python opens the file stream and loads its content into memory in a format your code can work with, such as a string or a list of lines.
Python does this with ease using built-in methods like read(), readline(), and readlines(). Python allows you to read files all at once or line by line. This means you can stop wasting time on repetitive tasks and start focusing on building logic that extracts real value from the data stored in files. This ease of use is one of the reasons why file handling in Python is a sought-after skill.
Note: To read a file in Python, you first need to open it in read mode using the open() function and set the access mode as ‘r’.
Syntax:
file = open('intellipaat.txt', 'r')
The table below lists all the access mode values in Python and their meanings.
Mode |
Description |
Use Case Example |
r |
Opens a file for reading |
Reading a plain text file (.txt, .log, etc.) |
rt |
Opens a file for reading in text mode |
Explicitly reads a file as text |
rb |
Opens a file for reading in binary mode |
Reading binary files like images, audio, or PDFs |
r+ |
Opens a file for reading and writing |
Read and update an existing text file |
rb+ |
Opens a file for reading and writing (binary) |
Modify parts of a binary file without overwriting all |
Once you have opened the file, Python provides several ways to read it.
Methods to Read a Text File in Python
All the read methods provided by Python serve a unique purpose. Python provides all the essential methods to work efficiently with text files.
- You might want to read the whole text in one go. You can achieve this using read() in Python.
- You might be looking to read one line at a time. For this, Python has the readline() function.
- If you want to break the entire text down into a list of lines, you can use the readlines() function.
- Python provides the file iteration method that allows you to read the text gradually, one line at a time, in a loop.
Let’s take a closer look at each method and understand how and when to use them.
read() Method in Python
The read() method reads the complete content of a text file as one long string. It is the most straightforward approach for accessing the whole file in one go.
Example:
Output:
Explanation: The whole file was loaded into the memory. It was printed all at once when the print() function was called.
When to Use:
- When the file is small and fits comfortably in memory.
- When you have to search, replace, or analyse the whole content at once.
Caution:
You should avoid using read() for large files (like 100 MB+), as it loads everything into memory, which can slow down your system or cause crashes.
readline() Method in Python
The readline() method reads just one line from the file each time it is called. It remembers the position of the line it read, so each subsequent call reads the next line.
Example:
Output:
Explanation: Unlike the read() function, the readline() function only prints the two lines that were asked to be printed.
When to Use:
- If you only want particular lines, such as the first lines.
- Whenever you need to read through formatted files, line by line
- When it comes to logs or some files where each line is a standalone sentence.
Caution:
The line ends with the new line character “n” when it is read from a file using the readline() method. This can lead to extra blank lines when printed. The aforementioned example demonstrates this. You can use .strip() function to remove trailing newline characters for cleaner output.
readlines() Method in Python
The readlines() method reads the entire file and returns a list of lines, where each element is a single line from the file.
Example:
Output:
Explanation: This function stores each line in the file as a separate string element in a list. You can now run operations on this list or process it.
When to Use:
- When each line needs to be processed independently.
- Whenever you want to reverse the lines or count how many there are.
- When the file size is small and can be managed by the memory.
Caution:
You should use the strip() function to clean up the output while iterating over the list because the readlines() method returns each line that ends with the “n” character.
Iteration Over the File Object in Python
This is the most memory-efficient way. Python lets you loop directly over the file object, reading one line at a time, without needing any of the methods mentioned earlier.
Example:
Output:
Explanation: This code minimises memory usage by opening the file and reading each line in turn using a loop. For a cleaner output, the strip() function eliminates any trailing newline characters.
When to Use:
- When you are working with large files like logs, datasets, and CSVs.
- When you want to process the file line by line without loading it fully into memory.
- This method is ideal for data processing pipelines or streaming scenarios.
Caution:
This method automatically handles memory optimisation and is typically preferred when reading big files.
Note: In all the examples above, we used a context manager (with) to open the files. This automatically takes care of closing the files. If you are not using with, remember to close the file.
Advanced File Reading Techniques in Python
These techniques are used when you want to read a file more specifically and want to customise it.
Reading Specific Lines in Python
If you want to extract a particular line at a particular position, you can use the enumerate function.
Example:
Output:
Explanation: In this code, we used enumerate to assign an integer to each line. The index starts from 0. Therefore, by assigning the index value of 1 to the variable i, we access the second line.
Reading Files in Chunks in Python
This method works well for files that are large, roughly 1 GB in size. Using this method, the file is divided into parts and worked on.
Example:
Output:
Explanation: In this example, we divided the file into chunks of 1024 bytes (1KB). Then, we read each chunk instead of the whole file.
Get 100% Hike!
Master Most in Demand Skills Now!
Reading Files using Generators in Python
By yielding one line at a time rather than returning all lines at once, generators let you develop unique, memory-efficient file readers. When processing really large files, streaming media, or creating scalable applications where memory usage is important, this method is perfect.
Example:
Output:
Explanation: The read_large_file() function is a generator that opens a file and fetches one cleaned-up line at a time. We used the keyword yield instead of return because it pauses the function and resumes from the same point when iteration continues. This way, only one line is held in memory at any given moment.
Practical Tips and Best Practices
Check the existence of the file:
To prevent runtime errors, you should always check whether the file exists before executing any operations on it, such as open(), read(), write(), etc.
Use Context Manager (with):
In each of the previously described cases, we used a context manager (with) to open the files. As a result, the files are automatically closed. Because, it prevents resource leaks and file corruption, closing the file is essential. If you are not using the context manager, remember to close the file.
Handling File Encodings:
Python by default uses the system’s default encoding, which is often UTF-8. Although some files use encodings like ISO-8859-1 or UTF-16. Use the open() method to explicitly specify encoding to prevent errors.
with open('intellipaat.txt', 'r', encoding='utf-8') as file:
content = file.read()
You can add a parameter named errors to the open() function if you are not sure how your file is encoded and you wish to suppress the UnicodeDecodeError. Set it to ignore.
with open('example.txt', 'r', encoding='utf-8', errors='ignore') as file:
content = file.read()
Common Errors and Exception Handling
File handling is not foolproof. Typically, the files you work with are big and prone to errors. Make sure your file handling code is contained within the try blocks to avoid the exceptions halting the entire code from functioning.
Python Made Easy – And Totally Free
Code at your own pace and build real projects
Conclusion
Reading text files with Python is a fundamental skill that all programmers should possess. Python provides modern and effective file management and data extraction techniques for all kinds of files. You can use the read() method to read through all the content of the files you have selected. This method is good when your file is smaller in size. However, readline() and enumerate() are the functions that you should employ if you need to read specific lines of the files, especially while working with large files.
To take your skills to the next level, check out this Python training course and gain hands-on experience. Also, prepare for job interviews with Python interview questions prepared by industry experts.
How to Read a File in Python – FAQs
Q1. How can I read a file's whole contents at once?
To load the entire contents of a file as a single string, use the read() method.
Q2. Is it possible to read a single line from a file?
Yes, you may read a line at a time with readline().
Q3. What happens if my file is too big to store in memory?
To minimise memory utilisation, you should loop through the file line by line.
Q4. After reading, do I have to manually close the file?
No, Python will automatically close the file for you if you use a with block.
Q5. When reading lines, how can I prevent receiving unnecessary newline characters?
To prevent unnecessary newline characters when reading lines, use the strip() method to remove them.