使用Python3查找字符串

2024-10-06 19:18:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我目前的代码:

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

我正在试图看到错误…我不确定。我的目标是找到一个字符串。 为什么我会得到这个:

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

我不知道如何以及为什么会出现这个错误。 有什么想法吗?非常感谢。 Credit to Kenly for posting the code :)


Tags: theinimportinputifosfnamedirectory
1条回答
网友
1楼 · 发布于 2024-10-06 19:18:03

没关系,这个问题解决了:) 这是新代码

#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()
    

现在已经解决了

相关问题 更多 >