Python get file line numbers

Python: Search strings in a file and get line numbers of lines containing the string

In this article, we are going to discuss, how to search for single or multiple strings in a file and get all the matched lines along with their line numbers.

Check if a string exists in a file

To check if a given string exists in the file or not, we have created a function,

def check_if_string_in_file(file_name, string_to_search): """ Check if any line in the file contains given string """ # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string if string_to_search in line: return True return False

It accepts a file path and a string as arguments. Then iterates over each line in the file one by one and for each line check if it contains the given string or not. If the line contains the given string, then return True. Whereas if no line in the file contains the given string, then it returns False.

Читайте также:  Как поставить на фон картинку в HTML

Contents of the file ‘sample.txt’ are,

Frequently Asked:

Hello this is a sample file It contains sample text Dummy Line A Dummy Line B Dummy Line C This is the end of file

Let’s check if this file contains a string ‘is’ or not,

# Check if string 'is' is found in file 'sample.txt' if check_if_string_in_file('sample.txt', 'is'): print('Yes, string found in file') else: print('String not found in file')

As file contains the ‘is’, therefore function check_if_string_in_file() returns True.

Here we get to know that file contains the given string or not. But what if we want to know all the exact occurrences of a string in the file like lines and line numbers. Let’s see how to do that,

Search for a string in file & get all lines containing the string along with line numbers

we have created a function, to get all the lines and line numbers which contain the given string,

def search_string_in_file(file_name, string_to_search): """Search for the given string in file and return lines containing that string, along with line numbers""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string line_number += 1 if string_to_search in line: # If yes, then add the line number & line as a tuple in the list list_of_results.append((line_number, line.rstrip())) # Return list of tuples containing line numbers and lines where string is found return list_of_results

It accepts a file path and a string as arguments. In the end, it returns a list of tuples, where each tuple contains the line number and line, which includes the given string.

How did it worked ?

  • Accept arguments – file path and a string to lookup.
  • Create an empty list of tuples.
  • Open the file at the given path in read-only mode.
  • Iterates over each line in the file one by one.
    • For each line, check if it contains the given string or not.
      • If the line contains the given string,
        • Creates a tuple of line number & the line and adds that to a list of tuples.

        Suppose we have a file ‘sample.txt’ and its contents are,

        Hello this is a sample file It contains sample text Dummy Line A Dummy Line B Dummy Line C This is the end of file

        Let’s get all the line along with line numbers which contain the word ‘is’,

        matched_lines = search_string_in_file('sample.txt', 'is') print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Line Number = ', elem[0], ' :: Line = ', elem[1])
        Total Matched lines : 2 Line Number = 1 :: Line = Hello this is a sample file Line Number = 6 :: Line = This is the end of file

        In total, there were two lines, which include the string ‘is’ and this function returned those lines along with their line numbers. Now suppose instead of search for a single string, we want to search for multiple strings in a file. Let’s see how to do that,

        Search for multiple strings in a file and get lines containing string along with line numbers

        To search for multiple strings in a file, we can not use the above-created function because that will open and close the file for each string. Therefore, we have created a separate function, that will open a file once and then search for the lines in the file that contains any of the given string i.e.

        def search_multiple_strings_in_file(file_name, list_of_strings): """Get line from the file along with line numbers, which contains any string from the list""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: line_number += 1 # For each line, check if line contains any string from the list of strings for string_to_search in list_of_strings: if string_to_search in line: # If any string is found in line, then append that line along with line number in list list_of_results.append((string_to_search, line_number, line.rstrip())) # Return list of tuples containing matched string, line numbers and lines where string is found return list_of_results

        Contents of the file ‘sample.txt’ are,

        Hello this is a sample file It contains sample text Dummy Line A Dummy Line B Dummy Line C This is the end of file

        Let’s get all the lines along with their line numbers which either contain the word ‘is’ or ‘what’,

        # search for given strings in the file 'sample.txt' matched_lines = search_multiple_strings_in_file('sample.txt', ['is', 'what']) print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2])
        Total Matched lines : 2 Word = is :: Line Number = 1 :: Line = Hello this is a sample file Word = is :: Line Number = 6 :: Line = This is the end of file

        The complete example is as follows,

        def check_if_string_in_file(file_name, string_to_search): """ Check if any line in the file contains given string """ # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string if string_to_search in line: return True return False def search_string_in_file(file_name, string_to_search): """Search for the given string in file and return lines containing that string, along with line numbers""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string line_number += 1 if string_to_search in line: # If yes, then add the line number & line as a tuple in the list list_of_results.append((line_number, line.rstrip())) # Return list of tuples containing line numbers and lines where string is found return list_of_results def search_multiple_strings_in_file(file_name, list_of_strings): """Get line from the file along with line numbers, which contains any string from the list""" line_number = 0 list_of_results = [] # Open the file in read only mode with open(file_name, 'r') as read_obj: # Read all lines in the file one by one for line in read_obj: line_number += 1 # For each line, check if line contains any string from the list of strings for string_to_search in list_of_strings: if string_to_search in line: # If any string is found in line, then append that line along with line number in list list_of_results.append((string_to_search, line_number, line.rstrip())) # Return list of tuples containing matched string, line numbers and lines where string is found return list_of_results def main(): print('*** Check if a string exists in a file *** ') # Check if string 'is' is found in file 'sample.txt' if check_if_string_in_file('sample.txt', 'is'): print('Yes, string found in file') else: print('String not found in file') print('*** Search for a string in file & get all lines containing the string along with line numbers ****') matched_lines = search_string_in_file('sample.txt', 'is') print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Line Number = ', elem[0], ' :: Line = ', elem[1]) print('*** Search for multiple strings in a file and get lines containing string along with line numbers ***') # search for given strings in the file 'sample.txt' matched_lines = search_multiple_strings_in_file('sample.txt', ['is', 'what']) print('Total Matched lines : ', len(matched_lines)) for elem in matched_lines: print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2]) if __name__ == '__main__': main()
        *** Check if a string exists in a file *** Yes, string found in file *** Search for a string in file & get all lines containing the string along with line numbers **** Total Matched lines : 2 Line Number = 1 :: Line = Hello this is a sample file Line Number = 6 :: Line = This is the end of file *** Search for a multiple string in a file and get lines containing string along with line numbers *** Total Matched lines : 2 Word = is :: Line Number = 1 :: Line = Hello this is a sample file Word = is :: Line Number = 6 :: Line = This is the end of file

        Источник

        How to print line numbers of a file in Python?

        EasyTweaks.com

        Use the Python enumerate function to loop through a text or csv file and then, for each line print the line number and the line values. Here’s a simple snippet:

        with open (your_file_path, 'r') as f: for i, line in enumerate (f): print(f'Line number:; Content: '.strip()) 

        Create an Example file

        Assuming that you have the following multi-line text written into a text file in your operating system (either Windows, Linux or macOS)

        This is our log file.
        It is located at the C:\MyWork directory.
        This file was built with Python.

        Find the line numbers and print the line contents

        Our task is to print for each line of the file the line number and the line contents.

        # import the path library - ships from Python 3.4 and onwards from pathlib import Path # replace with path of file in your environment file_path = Path('C:\WorkDir\multi_line_file.txt') # Verify that file exists if file_path.is_file(): #Open file and loop through lines, print line number and content with open (file_path, 'r') as f: for i, line in enumerate (f): print(f'Line number:; Content: '.strip()) else: print("The file doesn't exist.")

        This will return the following result:

        Line number:1; Content: This is our log file. Line number:2; Content: It is located at the C:\MyWork directory. Line number:3; Content: This file was built with Python.

        Next we would like to search for a specific string in the file contents and print only the specific line which contains the string. We will use the re (regular expressions) module to easily parse the file.

         with open (file_path, 'r') as f: for i, line in enumerate (f): # search for words ending with the string 'thon' if (re.search(r'thon\b', line)): print(f'Line number:; Content: '.strip()) 

        This will render the following result:

        Line number:3; Content: This file was built with Python.

        Get number of lines in a Python file

        To count the number of lines in a text file with Python, proceed as following:

        file_path = Path('C:\WorkDir\multi_line_file.txt') with open (file_path, 'r') as f: for l in file: l += 1 print (f'The file line count is : ') 

        Источник

Оцените статью