The piece of code below takes a string (userstring) and searches all .txt and .log files in a given directory for a match. However, I've added two other variables (userStrHEX & userStrASCII) where I convert the string to hex 开发者_运维百科and ascii to search all .txt and .log files against these string formats. Currently, these variables are not being used. I'm thinking to add these variables to a list, and maybe use a while loop to iterate through that section of the code up to the number of items in the list. Also, I think I will need to assign each item in the list to a static variable each time the loops is iterated ... I'm stuck! I would appreciate all insight. Thanks!
def do_search(self, line):
print " Directory to be searched: c:\Python27 "
directory = os.path.join("c:\\","Python27")
userstring = raw_input("Enter a string name to search: ")
userStrHEX = userstring.encode('hex')
userStrASCII = ' '.join(str(ord(char)) for char in userstring)
for root,dirname, files in os.walk(directory):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
f=open(os.path.join(root, file))
for line in f.readlines():
if userstring in line:
print "file: " + os.path.join(root,file)
break
else:
print "String NOT Found!"
break
f.close()
The best way is probably to create a regular expression that matches all three strings. Take a look at the documentation for the re module. In a nutshell:
regex = re.compile( "(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII ) )
Then instead of "userstring in line", check regex.search( line )
精彩评论