Find string in files python

Python Search for a String in Text Files

In this Python tutorial, you’ll learn to search a string in a text file. Also, we’ll see how to search a string in a file and print its line and line number.

After reading this article, you’ll learn the following 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
Читайте также:  Java class with 2 main methods

We will see each solution one by one.

Table of contents

How to Search for a String in Text File

Use the file read() method and string class find() method to search for a string in a text file. Here are the steps.

  1. Open file in a read modeOpen 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

I have a ‘sales.txt’ file that contains monthly sales data of items. I want the sales data of a specific item. Let’s see how to search particular item data in a sales file.

Читайте также:  Php fpm pm ondemand

sales 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')

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

Use the following steps if you are searching a particular text or a word in a file, and you want to print a line number and line in which it is 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: In this example, we’ll search the string ‘laptop’ in a file, print its line along with the line 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)
laptop string exists in a file line: laptop 10 15000 line number: 1

Note: You can also use the readline() method instead of readlines() 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.

Efficient way to search string in a large text file

All above way read the entire file in memory. If the file is large, reading the whole file in memory is not ideal.

In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text 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.

Note: The enumerate(file_pointer) doesn’t load the entire file in memory, so this is an efficient solution.

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
string found in a file Line Number: 1 Line: laptop 10 15000

mmap to search for a string in text file

In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text file.

Also, you can use the mmap module to find a string in a huge file. The mmap.mmap() method creates a bytearray object that checks the underlying file instead of reading the whole file in memory.

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')

Search string in multiple files

Sometimes you want to search a string in multiple files present in a directory. Use the below steps to search a text in all files of a directory.

  • 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

Search file for a list of strings

Sometimes you want to search a file for multiple strings. The below example shows how to search a text file for any words in a list.

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')

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

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 Finds the String in the File and Print

Python is an effective programming language utilized for data analysis, ML/machine learning, web development, and automation. “Searching for a string” in a particular file and extracting it is a common programming task. We can search for a specific word, phrase, or pattern in a text file and extract it for further analysis. In this article, we will discuss the approaches to finding a string in a file and printing it in Python.

The following methods are used to find the string in the file:

Method 1: Find the String in the File Using Python’s “Built-in File” Operations

To find a specific word, phrase, or pattern in a text file using Python, we can use some “built-in” file operations such as “open()”, “for” loop, and “if-else” statements. Here is an example:

The content in the original text file that will be dealt with is shown below:

The below code is utilized to locate the specified string and print it in Python:

with open ( ‘filename.txt’ , ‘r’ ) as f:
for line in f:
if ‘Python’ in line:
print ( line )
else:
print ( ‘String Not Found!’ )

  • The “txt” is the name of the file that we want to search for the string, and “Python” is the string that we want to find.
  • Now, open the file using the “open()” function and then go through each line in the file using a “for” loop.
  • For each line, we can check if the string we’re looking for is there using a special word called “in“.
  • Upon finding the string, print out the entire line.

Note: This method is simple and works well for small files. However, it may not be suitable for big files or when we need to perform complex string matching.

As analyzed, the string has been found and printed in the above output.

Method 2: Find the String in the File Using “Regular Expressions”

Regular expressions are like secret codes that we can utilize to find/search patterns in the specified text. The “re” library is utilized in Python to search for a string pattern by utilizing regular expressions.

The below code is used to find the string in the file and print it:

import re
search_pattern = ‘Python’
with open ( ‘filename.txt’ , ‘r’ ) as f:
for line in f:
match = re.search ( search_pattern, line )
if match:
print ( match.group ( ) )

In the above code snippet:

  • The “re” module is imported and a variable named “search_pattern” is initialized. This variable will be utilized to define the pattern of text to search for within the file.
  • Next, the file is opened using the “open()” function, which automatically handles closing the file once it’s been read.
  • The “for” loop goes over each line of text in the file. For each line, it uses the “search()” function to search for the pattern specified by the “search_pattern” variable.
  • If the pattern is found in the line, the “match” variable is set to the match object returned by the “re.search()” function.
  • Finally, if a match is found, the code prints the contents of the matched text via the “group()” method.

In the above output, the string has been located in accordance with the search pattern.

Method 3: Find the String in the File Using the “read()” Method

The “read()” method reads the full contents/data of a file into a string and the “find()” method is used to search for the string you want to find.

The following code will print the line number and line of the file where the specified string is found:

with open ( «filename.txt» , «r» ) as f:
lines = f.read ( )
for i, line in enumerate ( lines.splitlines ( ) ) :
if «Python» in line:
print ( i + 1 , line )

  • The “read()” method is utilized to read the entire/complete information of the file into a string variable called “lines”.
  • The “enumerate()” function is used to get the line number as well as the line itself.
  • The “if” statement is used to check if the word “Python” is in the line and in such a case, the line number and the line are printed to the console.

In this output, it can be seen that the string and its line number have been found and printed.

Method 4: Find the String in the File Using the “readlines()” Method

The “readlines()” method reads the contents of a file into a list of lines and the “for” loop is utilized to iterate through/over the list and search for the string you want to find.

The following code will print the line number and line of the file where the string is found:

with open ( «filename.txt» , «r» ) as f:
lines = f.readlines ( )
for i, line in enumerate ( lines ) :
if «Welcome» in line:
print ( i + 1 , line )

  • The file named “filename.txt” is opened in read mode (“r”) and allocates the file object to the initialized variable “f”.
  • A list of strings is created by reading the complete contents of the input file with the “readlines()” method.
  • The “for” loop iterates over the lines in the file, one at a time, for each line, and the “enumerate()” function is applied to get the line number and the line itself.
  • Now, the “if” statement is utilized to verify/check if the word “Welcome” is in the line.
  • If the particular word is in the line, the code prints the line number and the line to the console.

In the above snippet, the string “Welcome” along with the complete line and line number has been returned appropriately.

Conclusion

This Python guide explored different ways to find a string in a file and print it in Python such as “built-in file” operations, “regular expressions”, “read()” method, or the “readlines()” method. This blog demonstrated the approaches to finding the string in the file and printing it.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.

Источник

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