Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Python Search for a String in Text Files
    python

    Python Search for a String in Text Files

    March 23, 2023No Comments7 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Searching for a String in Python Source Code

    This Python guide will show you how to perform a text file search using a string. We’ll also look at how to print the line and number where a string was found in a file.

    Following this article, you will have gained knowledge in the following areas:

    cases.

    • If a file is small, read it into a string and use the

      find()

      method to check if a string or word is present in a file. (easier and faster than reading and checking line per line)
    • If a file is large, use the mmap to search a string in a file. We don’t need to read the whole file in memory, which will make our solution memory efficient.
    • Search a string in multiple files
    • Search file for a list of strings

    This is a step-by-step process in which we will examine each potential

    one.

    Table of contents


    • How to Search for a String in Text File


      • Example to search for a string in text file

    • Search file for a string and Print its line and line number

    • Efficient way to search string in a large text file

    • mmap to search for a string in text file

    • Search string in multiple files

    • Search file for a list of strings

    How to Search for a String in Text File

    To locate a specific phrase in a text file, you can use the find() function of the string class in conjunction with the read() method of the file. What follows is a

    steps.


    1. Open file in a read mode


      Open a file

      by setting a file path and access mode to the

      open()

      function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing. For example, r is for reading.

      fp= open(r'file_path', 'r')


    2. Read content from a file

      Once opened,

      read all content of a file

      using the

      read()

      method. The

      read()

      method returns the entire file content in string format.


    3. Search for a string in a file

      Use the

      find()

      method of a str class to check the given string or word present in the result returned by the

      read()

      method. The

      find()

      method. The find() method will return -1 if the given text is not present in a file


    4. Print line and line number

      If you need line and line numbers, use the

      readlines(

      ) method instead of

      read()

      method. Use the

      for loop

      and

      readlines()

      method to iterate each line from a file. Next, In each iteration of a loop, use the if condition to check if a string is present in a current line and print the current line and line number

    Example to search for a string in text file

    def search_str(file_path, word):
        with open(file_path, 'r') as file:
            # read all content of a file
            content = file.read()
            # check if string present in a file
            if word in content:
                print('string exist in a file')
            else:
                print('string does not exist in a file')
    search_str(r'E:\demos\files_demos\account\sales.txt', 'laptop')

    Produce Results

    :

    string exists in a file

    Search file for a string and Print its line and line number

    Follow these instructions if you want to print the line number and line where you found the text or word you were searching for in a file.

    present.

    • Open a file in a read mode.
    • Next, use the

      readlines()

      method to get all lines from a file in the form of a

      list

      object.
    • Next, use a loop to iterate each line from a file.
    • Next, In each iteration of a loop, use the

      if condition

      to check if a string is present in a current line and print the current line and line number.

    Example Here we’ll show how to search a file for the string “laptop,” print out the line containing that string along with the line number where it was found, and so on.

    number.

    # string to search in file
    word = 'laptop'
    with open(r'E:\demos\files_demos\account\sales.txt', 'r') as fp:
        # read all lines in a list
        lines = fp.readlines()
        for line in lines:
            # check if string present on a current line
            if line.find(word) != -1:
                print(word, 'string exists in file')
                print('Line Number:', lines.index(line))
                print('Line:', line)

    Produce Results

    :

    laptop string exists in a file
    line: laptop 10 15000
    line number: 1

    Efficient way to search string in a large text file

    These methods all work by loading the complete file into memory. For huge files, reading them entirely into memory is inefficient.

    See how to search for a string in a large text quickly and with minimal memory usage.

    file.

    • Open a file in read mode
    • 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 each line and line number.

    Keep in mind that this is a space-saving method because enumerate(file_pointer) doesn’t have to read the entire file. Example

    :

    with open(r"E:\demos\files_demos\account\sales.txt", 'r') as fp:
        for l_no, line in enumerate(fp):
            # search string
            if 'laptop' in line:
                print('string found in a file')
                print('Line Number:', l_no)
                print('Line:', line)
                # don't look for next lines
                break

    Conduct a Sample

    :

    string found in a file
    Line Number: 1
    Line: laptop 10 15000

    mmap to search for a string in text file

    Here we’ll examine the most time- and memory-efficient strategy for searching for a string in a huge text file.

    As an alternative, the mmap module can be used to search for a string within a very large file. Instead of reading the entire file into memory, the mmap.mmap() method generates a bytearray object that checks the underlying file. Example

    :

    import mmap
    with open(r'E:\demos\files_demos\account\sales.txt', 'rb', 0) as file:
        s = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
        if s.find(b'laptop') != -1:
            print('string exist in a file')

    Produce Results

    :

    string exist in a file

    Search string in multiple files

    It’s helpful to be able to perform a string search on a number of files within a directory at once. Follow the instructions below to do a text search across all of a

    directory.

    As an Illustration,


    • List all files of a directory
    • Read each file one by one
    • Next, search for a word in the given file. If found, stop reading the files.

    :

    import os
    dir_path = r'E:\demos\files_demos\account'
    # iterate each file in a directory
    for file in os.listdir(dir_path):
        cur_path = os.path.join(dir_path, file)
        # check if it is a file
        if os.path.isfile(cur_path):
            with open(cur_path, 'r') as file:
                # read all content of a file and search string
                if 'laptop' in file.read():
                    print('string found')
                    break

    Produce Results

    :

    string found

    Search file for a list of strings

    It’s helpful to be able to search a file for more than one string at a time. The code snippet below demonstrates how to perform a generic word search in a text file. Example

    :

    words = ['laptop', 'phone']
    with open(r'E:\demos\files_demos\account\sales.txt', 'r') as f:
        content = f.read()
    # Iterate list to find each word
    for word in words:
        if word in content:
            print('string exist in a file')

    Produce Results

    :

    string exist in a file
    Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language Python Search for a String in Text Files
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleFamous Python Certification Courses for Finance
    Next Article Lists and Tuples in Python

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.