- How do you read a specific line of a text file in Python?
- 5 Answers 5
- Edit:
- Read Specific Lines From a File in Python
- Table of contents
- Steps To Read Specific Lines From A File
- Example: Read specific lines from file by line number
- linecache Module Read line from a file by line number
- Use readlines() to Read the range of line from the File
- Generator to Read Lines from a file by line number
- for Loop in fileobject to Read Specific Lines in Python
- Conclusion
- About Vishal
- Related Tutorial Topics:
- Python Exercises and Quizzes
- Python : select a specific line from text
How do you read a specific line of a text file in Python?
I’m having trouble reading an entire specific line of a text file using Python. I currently have this:
load_profile = open('users/file.txt', "r") read_it = load_profile.readline(1) print read_it
Of course this will just read one byte of the first line, which is not what I want. I also tried Google but didn’t find anything.
5 Answers 5
What are the conditions of this line? Is it at a certain index? Does it contain a certain string? Does it match a regex?
This code will match a single line from the file based on a string:
load_profile = open('users/file.txt', "r") read_it = load_profile.read() myLine = "" for line in read_it.splitlines(): if line == "This is the line I am looking for": myLine = line break print myLine
And this will give you the first line of the file (there are several other ways to do this as well):
load_profile = open('users/file.txt', "r") read_it = load_profile.read().splitlines()[0] print read_it
load_profile = open('users/file.txt', "r") read_it = load_profile.readline() print read_it
file.readline([size])
Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). [6] If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. When size is not 0, an empty string is returned only when EOF is encountered immediately.
Note Unlike stdio‘s fgets(), the returned string contains null characters (‘\0’) if they occurred in the input.
file.readlines([sizehint])
Read until EOF using readline() and return a list containing the lines thus read. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface may choose to ignore sizehint if it cannot be implemented, or cannot be implemented efficiently.
Edit:
Answer to your comment Noah:
load_profile = open('users/file.txt', "r") read_it = load_profile.read() myLines = [] for line in read_it.splitlines(): # if line.startswith("Start of line. "): # if line.endswith(". line End."): # if line.find("SUBSTRING") > -1: if line == "This is the line I am looking for": myLines.append(line) print myLines
Read Specific Lines From a File in Python
This article lets you know how to read a specific lines from a file by line number in Python.
Table of contents
Steps To Read Specific Lines From A File
Let’s assume the file to read is significantly large (in GB), and you don’t want to read the whole file in memory at once but only want to jump and read lines #5 and #120. To read specific lines from a text file, Please follow these steps:
- Open file in Read Mode To open a file pass file path and access mode r to the open() function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing.
For example, fp= open(r’File_Path’, ‘r’) to read a file. - Create a list to store line numbers Create a list with the number of each line in a text file to read.
For example, line_numbers = [4, 7] . Here we are reading lines 4 and 7. - Create a list to store lines After reading line 4 and 7 we will store result it in a list variable.
- Use for loop with enumerate() function to get a line and its number. The enumerate() function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by the open() function to the enumerate() .
We can use this enumerate object with a for loop to access the line number.
Note: enumerate(file_pointer) doesn’t load the entire file in memory, so this is an efficient solution. - Read file by line number Use the if condition in each iteration of a loop to check the line number. If it matches, then save that line into a list.
Example: Read specific lines from file by line number
The following code shows how to read a text file by line number in Python. See the attached file used in the example and an image to show the file’s content for reference.
In this example, we are reading line number 4 and 7 and storing it in a list variable.
with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # lines to read line_numbers = [4, 7] # To store lines lines = [] for i, line in enumerate(fp): # read line 4 and 7 if i in line_numbers: lines.append(line.strip()) elif i > 7: # don't read after line 7 to save time break print(lines)
linecache Module Read line from a file by line number
In this section, we’ll see how to read file by line number using a linecache module.
Python’s linecache is another performance-optimized way to jump to a particular line in a text file. Assume if you have a large text file, then linecache is the correct choice.
The linecache module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. This is used by the traceback module to retrieve source lines for inclusion in the formatted traceback.
Python Documentation
Use the linecache.getline() method to read specific line from a file.
linecache.getline(filename, lineno, module_globals=None)
- Get line lineno from a file named filename . This function will not return any error if the line is not present in a file instead, it will return an empty string.
- Change the lineno to your desired line number, and you’re ready to go.
import linecache # read fifth line line = linecache.getline(r"E:\demos\files\read_demo.txt", 5) print(line)
Note: The linache reads the whole file in memory. So, if random access to line number is more important than performance, then use linache.
If you want to read more than one line number from a file using linecache use the below example.
import linecache line_numbers = [2, 4] lines = [] for i in line_numbers: x = linecache.getline(r"E:\demos\files\read_demo.txt", i).strip() lines.append(x) print(lines)
Use readlines() to Read the range of line from the File
If your file size is small and you are not concerned with performance, then the readlines() method is best suited.
Reading a file in Python is fast if the file size is in a few MB.
The readlines() method reads all lines from a file and stores it in a list. You can use an index number as a line number to extract a set of lines from it.
This is the most straightforward way to read a specific line from a file in Python. We read the entire file using this way and then pick specific lines from it as per our requirement.
Use readlines()[start:end] to read range of lines.
- the start is the starting line number
- the end is the last line number
- To read from line number 3 to 5 use readlines()[2:5]
- To read a single line use fp.readlines()[2] . this will read the third line.
Example: Read line from 3 to 5
with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line number 3 to 5 # index starts from 0 x = fp.readlines()[2:5] print(x)
Example: Read line 8
with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line 8 x = fp.readlines()[7] print(x)
You can also use the readline() method to read a file line by line, stop when you’ve gotten to the lines you want. Using this technique, we don’t need to read the entire file.
lines = [2, 5] result = [] i = 0 with open("read_demo.txt", "r+") as fp: # access each line while True: line = fp.readline() # check line number if i in lines: result.append(line.strip()) # line number greater than needed exit the loop # lines[-1] give last item from list if i > lines[-1]: break; i = i + 1 print(result)
Generator to Read Lines from a file by line number
A fast and compact solution could be a generator expression to read a file using the line numbers.
If the number of lines to be returned from a file vast, you should use the generator.
This solution accepts file pointer and line numbers to be read returns a generator object to iterate using a loop to get each line.
# read by line numbers def get_lines(fp, line_numbers): return (x for i, x in enumerate(fp) if i in line_numbers) with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line 4 and 7 lines = get_lines(fp, [3, 6]) # print each line for line in lines: print(line.strip())
for Loop in fileobject to Read Specific Lines in Python
If your file size is small and you are not concerned with performance, then use looping technique.
- Use for loop to iterate each line from a file object
- Use the if condition in each iteration of a loop to check the line number. If it matches, then save that line into a list.
Note: This will load entire file in memory.
lines = [2, 5] result = [] i = 0 with open("read_demo.txt", "r+") as fp: # access each line for line in fp: # check line number if i in lines: result.append(line.strip()) i = i + 1 print(result)
Conclusion
- Use readlines() or readline() and loop to iterate lines from a file object if the file size is small.
- Use linecache for a more clean solution. It is fast if you are reading repeatedly or reading different lines from multiple files.
- Use a loop and enumerate() for large files because we don’t need to load the entire file in memory.
Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.
About Vishal
I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter
Related Tutorial Topics:
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
- 15+ Topic-specific Exercises and Quizzes
- Each Exercise contains 10 questions
- Each Quiz contains 12-15 MCQ
Python : select a specific line from text
For better memory usage you can use enumerate for line number tracking:
str1 = "Start" fp = open("C. test.txt") check = 0 for i,line in enumerate(fp): if str1 in line: check = i continue if "contain" in line and (i == check + 3): print "OK" else: print "NOK"
Here i == check + 3 condition will check your 3rd line condition.
This has a disadvantage: Assume this: line 1, 2 contain Start , line 4 has contain , this method would miss .
Might be small overhead, but if your file isn’t too big, I’d just dump every line in a list L, then loop through that list — if row r starts with str1, you can just do L[r+3] and check if it contains ‘contain’.
Use two list to store the positions match your rule.
Then check out the position match your relative offset.
start_lines = [] contains_lines = [] with open("C. test.txt") as inp: line_num = 0 for line in inp: if line.startswith("start"): start_lines.append(line_num) if "contains" in line: contains_lines.append(line_num) line_num += 1 print [line_num for line_num in contains_lines if line_num - 3 in start_line]
@Mehdi ouahabi you motioned that «go to the line that start with Start«, so we check only the lines those start with «Start», and not those contain Start in the middle or at the end :
with open("test.txt") as file: for line in file: if line.startswith("Start"): #instead of "str1 in line" if "contain" in line: print "OK" else: print "NOK"
***EDIT:***in this case you will check if a line start/ contain today’s date
from datetime import date with open("test.txt") as file: for line in file: #if str(date.today()) in line:#check if today's date exist in the line if line.startswith(str(date.today())): #check if the line start with today's dates if "contain" in line: print "OK" else: print "NOK"