Python script file search

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

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
Читайте также:  Guess mime type python

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.

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 Script to search for a file

If yes accept and upvote Solution 3: This simple code will do the work Solution 4: I hope this will help you Question: How can we write code in Python​ to find all file system including files from root to each and every file of the current computer system. Im still a learner in python, and while studying this code, i encountered the following questions: In the lines below, what is the implication of the line «mode=stat.SIMODE(os.lstat(file)[stat.

Python Script to search for a file

i was going through one of the python scripts that Searches for Files and lists their permissions. Im still a learner in python, and while studying this code, i encountered the following questions:

In the lines below, what is the implication of the line «mode=stat.SIMODE(os.lstat(file)[stat.ST_MODE])» ? what is the value returned to «mode» ? and how does it function in providing information of the permissions? Would be grateful if someone could explain this.

Also, I need to understand how the nested for loops within this segment work with respect to getting the desired output of diplaying the filenames and associated permissions ?

And what is the significance of «level» here ?

Would be very grateful, if anyone could answer the above questions and give any relevant guidance. Thanks in advance.

import stat, sys, os, string, commands try: #run a 'find' command and assign results to a variable pattern = raw_input("Enter the file pattern to search for:\n") commandString = "find " + pattern commandOutput = commands.getoutput(commandString) findResults = string.split(commandOutput, "\n") #output find results, along with permissions print "Files:" print commandOutput print "================================" for file in findResults: mode=stat.S_IMODE(os.lstat(file)[stat.ST_MODE]) print "\nPermissions for file ", file, ":" for level in "USR", "GRP", "OTH": for perm in "R", "W", "X": if mode & getattr(stat,"S_I"+perm+level): print level, " has ", perm, " permission" else: print level, " does NOT have ", perm, " permission" except: print "There was a problem - check the message above" 

The interactive Python interpreter shell is a good place to play around with snippets of Python code in order to understand them. For instance, to get the mode thing in your script:

>>> import os, stat >>> os.lstat("path/to/some/file") posix.stat_result(st_mode=33188, st_ino=834121L, st_dev=2049L, . >>> stat.ST_MODE 0 >>> os.lstat("path/to/some/file")[0] 33188 >>> stat.S_IMODE(33188) 420 

Now you know the values, check the Python docs to get their meaning.

In a similar way you could try to answer the other questions yourself.

UPDATE: The value of mode is a bitwise OR combination of different mode flags. The nested loop «manually» builds the names of these flags, uses getattr to get their values and then checks if mode includes these values.

Search files in all drives using Python, The sample below shows a regular expression search. import os import re import win32api def find_file (root_folder, rex): for root,dirs,files in os.walk (root_folder): …

Search data in file using python?

i have a file file.txt i want to search a data in a file using phone number for example if i entered 996452544 then result will be Alex 996452544 alex@gmail how to do that in python i do not know i am newbie help me.

Alex 996452544 alex@gmail Jhon 885546694 jhon@gmail Arya 896756885 arya@gmail 
def searchContact(): number=raw_input("Enter phone number to search data : ") obj1=open("file.txt","r") re=obj1.read() print re obj1.close() searchContact() 
def searchContact(): obj1 = open("address.txt","r") number = raw_input("Enter phone number to search data : ") for line in obj1.readlines(): if number in line: print line obj1.close() 

hope this help. If yes accept and upvote

def searchContact(): number=raw_input("Enter phone number to search data : ") obj1=open("file.txt","r") re=obj1.read() print re x= re.split("\n") matching = [s for s in x if number in s] print matching obj1.close() searchContact() 

This simple code will do the work

def searchContact(): number=raw_input("Enter phone number to search data : ") obj1=open("file.txt","r") for line in obj1.readlines(): if number in line: print(line) obj1.close() 

I hope this will help you

 for line in obj1.readlines(): if number in line: print line 

What’s the fastest way to recursively search for files in, Yeah, but lots of people still use python 2.7 which doesn’t have these advantages. I ran a test case with os.walk and glob.iglob and walk was 20 % faster than iglob …

Find all directories and files of your laptop

How can we write code in Python​ to find all file system including files from root to each and every file of the current computer system.

You can use the os.walk method. Here is an example:

# !/usr/bin/python import os for root, dirs, files in os.walk(".", topdown=False): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name)) 

copied from http://www.tutorialspoint.com/python/os_walk.htm

See the full documentation: https://docs.python.org/2/library/os.html#os.walk

Refer to the os module.
os.chdir(path) changes the working dir.
os.listdir() lists all the directories in your working dir.

If you wanna find all the directories of your current system, you need to iterate through your system using for example BFS search (https://en.wikipedia.org/wiki/Breadth-first_search).

Open a File in Python, A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes …

Источник

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