All Courses

Searching in Python Forensics

Vicky Bal

9 months ago

Python Forensics | Insideaiml
Table of Contents
  • What is Searching in forensics?
  • Examples
              1. How to search for a string in text files
              2. String exist in the file or not
              3. First string contains the second one or not

What is Searching in forensics?

          One of the most important parts of forensic investigation is the search, which usually boils down to finding out who is carrying out the evidence.
Search is a key word of the message, with the help of this key word we can look for evidence. We can do our tasks with some knowledge and experience knowing what to look for in a file along with the files that have been deleted.
Python provides us with a mechanism built into a standard library to carry out our search task. With the help of the search we find answers to questions like “who”, “what”, “where” and “when” and so on.
Let’s take an example

Examples

1. How to search for a string in text files

          We search for a character string line by line. If the string is found, we output that string and the line number.  
insideaiml.txt
InsideAIML is Good Plateform for AI & ML. 
Anyone who wants to make their Carrier in AIML can join here. 
They started teaches us from Zero.
Coding
 # opening a text file
text_file = open("insideaiml.txt", "r")
  
# setting flag and index to 0
flag = 0
index = 0
 
keyword = 'join'
# Loop through the file line by line
for line in text_file:  
    index += 1 
      
    # checking string is present in line or not
    if keyword in line:
        
      flag = 1
      break 
          
# checking condition for string found or not
if flag == 0: 
   print('String', text_file, 'Not Found') 
else: 
   print('String', text_file, 'Found In Line', index)
  
# closing text file    
text_file.close() 
Output
String <_io.TextIOWrapper name='insideaiml.txt' mode='r' encoding='cp1252'> 
Found In Line 2

2. String exist in the file or not

          We only exist a string in the file or not.
keyword = 'AIML'
  
# opening a text file
text_file = open("insideaiml.txt", "r")
  
# read file content
read_file = text_file.read()
  
# checking condition for string found or not
if keyword in read_file: 
    print('String', keyword, 'Found In File')
else: 
    print('String', keyword, 'Not Found') 
  
# closing a file
text_file.close() 
Output
String AIML Found In File

3. First string contains the second one or not

          In the example take taken here, we have taken two strings and used find function to check whether the first string contains the second one or not.
# Searching a particular word from a given string
str1 = "This is a InsideAIML. How may I help you"
str2 = "InsideAIML"

print(str1.find(str2))
print(str1.find(str2, 10))
print(str1.find(str2, 40))
The above program output-
10
10
-1
Python provides a function “find” which helps us to search a message or a paragraph.
   
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our insideAIML blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live insideAIML discussion forum.
Keep Learning. Keep Growing.

Submit Review