Read from a Specific Position in a File in Python
In Python, you can read from a specific position in a file using the seek()
method, which moves the file pointer to the desired byte position. Once positioned, you can use read()
or readline()
to retrieve data from that location. This tutorial covers multiple ways to achieve this with examples.
Examples
1. Using seek()
to Read from a Specific Byte Position
The seek()
method moves the file pointer to a specified byte position, allowing us to read from that point onwards.
In this example, we open example.txt, seek the file pointer to 10th byte, and then read next 20 characters.

main.py
# Opening a file in read mode
with open("example.txt", "r") as file:
file.seek(10) # Move to the 10th byte
content = file.read(20) # Read next 20 characters
print("Extracted Content:", content)
Explanation
Here’s how the program works:
open("example.txt", "r")
: Opens the file in read mode. Reference: open() builtin function.seek(10)
: Moves the file pointer to the 10th byte.read(20)
: Reads the next 20 characters from the file.
Output (Example):

This extracts content starting from the 10th byte and reads the next 20 characters.
2. Using seek()
and readline()
to Read a Specific Line
You can also use seek()
to position at a specific byte and then read a full line using readline()
.

main.py
# Opening the file
with open("example.txt", "r") as file:
file.seek(30) # Move to byte position 30
line = file.readline() # Read the line from that position
print("Extracted Line:", line.strip())
Explanation
seek(30)
: Moves to the 30th byte in the file.readline()
: Reads and returns the entire line from the current pointer position.strip()
: Removes any leading/trailing whitespace or newline characters.
Output (Example):

This ensures that the program fetches a complete line starting from byte 30.
3. Reading from the Last 50 Bytes of a File
You can use seek()
with os.SEEK_END
to move the pointer to the last 50 bytes of a file and read from there.

main.py
with open("example.txt", "rb") as file: # Open in binary mode
file.seek(-10, 2) # Move 10 bytes before the end
content = file.read().decode("utf-8") # Read till the end
print("Extracted Content:", content)
Explanation
open("example.txt", "rb")
: Opens the file in binary mode to allow negative seeking.seek(-10, 2)
: Moves the pointer 10 bytes before the end (2
representsos.SEEK_END
).read()
: Reads the last 10 bytes.decode("utf-8")
: Converts binary content to a readable string.
Output (Example):

This approach is useful for reading logs or recent updates in a file.
4. Reading from a Specific Line Number
Instead of using byte positions, you can read from a specific line number by iterating over the file.

main.py
# Define the line number to read
target_line = 2
# Open file and read specific line
with open("example.txt", "r") as file:
for current_line, line_content in enumerate(file, start=1):
if current_line == target_line:
print("Line", target_line, ":", line_content.strip())
break
Explanation
enumerate(file, start=1)
: Reads the file line by line while tracking the line number.if current_line == target_line
: Stops reading when the target line is reached.strip()
: Removes unwanted whitespace.
Output (Example):

This method is ideal when dealing with line-based text processing.
Conclusion
In this tutorial, we covered several ways to read from a specific position in a file:
- Using
seek()
withread()
– Reads from a specific byte position. - Using
seek()
withreadline()
– Reads a full line from a specific byte position. - Reading the last few bytes using
seek(-N, 2)
– Useful for log files. - Reading a specific line by iteration – Ideal for line-based reading.