0

This is my code so far:

import os

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


user_input = input('What is the name of your directory')
directory = os.listdir(user_input)

searchstring = "import os"

for fname in directory:
    if os.path.isfile(user_input + os.sep + fname):
        # Full path
        f = open(user_input + os.sep + fname, 'r')
        if searchstring in f.read():
            print(bcolors.OKGREEN + '[-]' + bcolors.ENDC + 'String found in file' % fname )
        else:
            print(bcolors.FAIL + '[+]' + bcolors.ENDC + 'String not found in file  %s' %fname)
        f.close()

Im trying to see the error...I'm not sure. My goal is for this to find a string. Why do i get this:

Traceback (most recent call last):
  File "/Users/jl/Downloads/Simple-Adware-master/src/adware/findstring.py", line 23, in <module>
    if searchstring in f.read():
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte

I don't know how and why I get this error. Any ideas? It'll be appreciated. Credit to Kenly for posting the code :)

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • It's probably found a binary file. – alani Sep 10 '20 at 16:51
  • You could try the other way round - open the file as binary (use `'rb'` instead of `'r'`) and then try to find the encoded search string inside the bytes (`if searchstring.encode() in f.read():`) - rather than trying to decode the bytes from the file – alani Sep 10 '20 at 16:55
  • What happens if you try to write the path without using an input, if you get an Unicode error again you can try r'your_path'. – jairoar Sep 10 '20 at 16:55
  • 1
    @jairoar That's not the problem. The problem is that it has been opened as a text file with `'r'` so `read` tries to return a string, so it is trying to decode the bytes as utf-8 and failing. My guess is that it has found a binary file in the directory. Printing the filename would be a good first debugging step. – alani Sep 10 '20 at 16:57
  • Please do not add [SOLVED] in the question. There is a green tick arrow on the left side of the answer which you can use to mark the question as solved – Dharman Sep 10 '20 at 20:36
  • ok, but i have to wait 2 days :( – Biggyboiii Sep 10 '20 at 20:43

1 Answers1

0

Never mind, this question is solved :) Here is the new code

#Import os module
import os
from time import sleep

#The colours of the things
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

# Ask the user to enter string to search
search_path = input("Enter directory path to search : ")
file_type = input("File Type : ")
search_str = input("Enter the search string : ")

# Append a directory separator if not already present
if not (search_path.endswith("/") or search_path.endswith("\\") ): 
        search_path = search_path + "/"
                                                          
# If path does not exist, set search path to current directory
if not os.path.exists(search_path):
        search_path ="."

# Repeat for each file in the directory  
for fname in os.listdir(path=search_path):

   # Apply file type filter   
   if fname.endswith(file_type):

        # Open file for reading
        fo = open(search_path + fname, 'r')

        # Read the first line from the file
        line = fo.read()

        # Initialize counter for line number
        line_no = 1

        # Loop until EOF
        if line != '' :
                # Search for string in line
                index = line.find(search_str)
                if ( index != -1) :
                    print(bcolors.OKGREEN + '[+]' + bcolors.ENDC + ' ', fname, sep="")
                    print('      ')
                    sleep(0.01)
                else:
                    print(bcolors.FAIL + '[-]' + bcolors.ENDC + ' ',  fname, ' ', 'does not contain', ' ', search_str, sep="")
                    print("       ")
                    sleep(0.01)
                line = fo.readline()  

                # Increment line counter
                line_no += 1
        # Close the files
        fo.close()
    

It is now solved.