Table of Contents
What Will You Learn
In this tutorial, you'll explore Python's file handling capabilities, focusing on reading text files. You'll learn how to open files using the open()
function, read entire files or specific lines with read()
, readline()
, and readlines()
, and manage file resources efficiently using the with
statement. The guide also covers best practices for handling different file encodings and error management, equipping you with the skills to read and process text files effectively in Python.
Being able to read data from a text file is one of the most fundamental skills in Python programming. It’s essential for tasks like reading logs, configuration files, user input, and data for analysis. Whether you’re building a script to process CSV reports or parsing logs from a server, you’ll need to understand how to open and read files safely and efficiently.
Python provides a powerful set of tools for file handling that are both easy to use and flexible. Learning how to use these tools correctly — especially the
with
statement and file reading methods — is crucial for writing reliable and professional-quality scripts.
How to Read Data from a Text File in Python?
The most common way to read data from a file in Python is using the built-in open()
function. You should always use it in combination with the
with
statement to ensure that the file is automatically closed. There are three main methods to read content: read()
reads the entire file as one
string, readline()
reads one line at a time, and readlines()
reads the file into a list of lines.
Below are practical examples showing how to read text from a file in Python.
# Example 1: Read the entire file
with open("sample.txt", "r") as file:
content = file.read()
print(content)
# Example 2: Read file line by line
with open("sample.txt", "r") as file:
for line in file:
print(line.strip())
# Example 3: Read all lines into a list
with open("sample.txt", "r") as file:
lines = file.readlines()
print(lines) # ['First line\n', 'Second line\n', ...]
Understanding how to read data from a text file in Python gives you access to external content and makes your programs far more useful.
Reading Specific Content from a File
Sometimes you don’t need the entire file — just specific parts. Python makes it easy to read a particular line, word, number, or even structured data like a matrix or dictionary.
This is useful when you’re working with large files or want to extract only targeted content. You can use line indexes, split()
, and other built-in functions to
access and parse data in a structured way. Let’s go through practical use cases.
Read a Specific Line
To read a specific line from a file, first read all lines using readlines()
or convert the file into a list with list(file)
. Then access the desired
line by its index. Remember that indexes start from 0. Also, make sure the file contains enough lines to avoid IndexError
.
with open("data.txt", "r") as file:
lines = file.readlines()
third_line = lines[2] # 3rd line (index 2)
print(third_line.strip())
Read Words or Tokens
To read individual words or tokens from a file, loop through each line and use the split()
method. This breaks a string into words based on spaces by default. You
can also split by commas or other delimiters for more structured files. This is useful for text parsing, keyword searches, or token-based processing.
with open("text.txt", "r") as file:
for line in file:
words = line.split()
print(words) # ['This', 'is', 'a', 'line']
Read Numbers or Integers
If your file contains numeric data, read each line and convert the tokens into integers using int()
. You can use list comprehensions to keep your code clean and
efficient. Always ensure you strip whitespace and check for empty lines. This is helpful in data analysis and numerical processing tasks.
with open("numbers.txt", "r") as file:
for line in file:
nums = [int(n) for n in line.strip().split()]
print(nums) # [1, 2, 3]
Read a Matrix (2D list)
Reading a matrix involves reading each line and splitting it into elements, then storing those lists inside a master list. This technique is widely used in competitive
programming, image processing, and simulations. Each row of the file becomes one sublist in the matrix. Use int()
or float()
to cast values as needed.
matrix = []
with open("matrix.txt", "r") as file:
for line in file:
row = [int(n) for n in line.strip().split()]
matrix.append(row)
print(matrix) # [[1, 2], [3, 4], [5, 6]]
Read a Dictionary or Key-Value Pairs
If your file contains key-value pairs (like name: John
), read each line, split it by the delimiter (e.g. colon), and store the results in a dictionary. This method
is useful for reading configuration files, user data, or metadata. Always strip whitespace to avoid errors and handle malformed lines safely.
data = {}
with open("config.txt", "r") as file:
for line in file:
if ":" in line:
key, value = line.strip().split(":", 1)
data[key.strip()] = value.strip()
print(data) # {'name': 'John', 'age': '30'}
Common Mistakes When Reading from Text Files in Python
Forgetting to Close the File
A frequent beginner mistake is opening a file with open()
but not closing it afterward. This leads to resource leaks and file locks, especially on Windows systems.
Always use the with
statement, which automatically closes the file when you're done. This ensures safe and clean file handling, even if an error occurs
mid-execution.
# Wrong
file = open("data.txt", "r")
content = file.read()
# file.close() is missing
# Correct
with open("data.txt", "r") as file:
content = file.read()
Reading the File Multiple Times
Once a file is read using read()
, the file pointer moves to the end. If you try to read it again without resetting the pointer using seek(0)
, you'll get
an empty string. This is a common issue when beginners try to call multiple read()
or readlines()
in a row.
# Wrong
with open("file.txt", "r") as f:
print(f.read())
print(f.read()) # Will print nothing
# Correct
with open("file.txt", "r") as f:
print(f.read())
f.seek(0)
print(f.read())
Ignoring Whitespace and Newlines
Beginners often forget to strip trailing newline characters when processing lines from a file. This can cause visual issues in output or problems when comparing strings. Always
use strip()
or rstrip()
to clean up lines after reading.
# Wrong
with open("data.txt") as f:
for line in f:
print("Line:", line) # Includes '\n'
# Correct
with open("data.txt") as f:
for line in f:
print("Line:", line.strip())
Using Wrong File Path
Another common error is trying to open a file using a wrong or relative path. This leads to FileNotFoundError
. Always check the current working directory and use
absolute paths for better reliability, especially when running scripts from different locations.
# Wrong
open("data.txt", "r") # Fails if file not in same directory
# Correct
open("/absolute/path/to/data.txt", "r")
Treating All Content as Strings
File content is read as strings by default. Beginners often forget to convert strings to numbers before performing arithmetic operations. This leads to type errors or incorrect
logic. Use int()
or float()
to safely convert data after reading.
# Wrong
with open("nums.txt") as f:
for line in f:
total = line + 10 # TypeError
# Correct
with open("nums.txt") as f:
for line in f:
total = int(line.strip()) + 10
Frequently Asked Questions (FAQ)
How do I read data from a text file in Python?
To read data from a text file in Python, use the built-in open()
function along with a file reading method like read()
, readline()
, or
readlines()
. The most recommended way is to use the with
statement to ensure the file is automatically closed. For example:
with open("file.txt", "r") as f:
content = f.read()
print(content)
This approach prevents file locking and ensures good resource management. You can also loop through lines using for line in f:
if you want to process content line
by line. Always remember to strip newline characters if needed, and use proper encoding for non-ASCII text.
What’s the difference between read(), readline(), and readlines()?
These are three different methods for reading content from a file. read()
returns the entire content of the file as a single string. readline()
reads
just the next line each time it's called. readlines()
reads all lines and returns them as a list of strings. Use read()
when you want everything at
once, readline()
for single-line processing, and readlines()
if you need random access to each line.
with open("data.txt") as f:
all_text = f.read()
first_line = f.readline()
all_lines = f.readlines()
Choose the method based on your memory constraints and logic needs. For large files, avoid read()
to prevent high memory usage.
How do I read a file line by line in Python?
To read a file line by line, use a for
loop inside a with open()
block. This is the most memory-efficient way to process large files, as it reads one
line at a time without loading the entire file into memory. Here’s how:
with open("log.txt", "r") as file:
for line in file:
print(line.strip())
Use strip()
to remove trailing newline characters. This approach is ideal for reading logs, processing CSV files, or parsing line-based input efficiently.
Why do I get FileNotFoundError when reading a file?
A FileNotFoundError
occurs when Python cannot locate the file you’re trying to open. This usually means the file path is incorrect or the file doesn’t exist in the
working directory. Always double-check your path and filename. If you’re using a relative path, make sure the file is in the same directory where you’re running the script.
# Safe pattern
with open("C:/data/input.txt", "r") as file:
print(file.read())
Alternatively, print the current working directory using import os; print(os.getcwd())
to confirm where Python is looking. Use absolute paths for reliability in
production scripts.
How can I read only numbers from a text file?
To read only numbers from a text file, read each line, split it into tokens, and convert each token to int
or float
. This is useful for numeric
datasets or scientific processing. Be sure to strip the line before splitting and handle any possible formatting errors.
with open("numbers.txt", "r") as file:
for line in file:
numbers = [int(n) for n in line.strip().split()]
print(numbers)
If the file contains non-numeric values, wrap conversion in try/except
blocks or use filtering to skip bad input. This technique works well for matrices and
line-based numeric data.