我正在尝试在文件中查找几个字符串,但 line.find() 不会为文件中的任何字符串返回 true。请看一下建议的内容。搜索必须是连续的,我需要保留偏移量找到的每个字符串的值。并且搜索下一个字符串应该从该偏移量开始。
def CheckFile(*argv):
import os
Filename = argv[0]
Search = argv[1]
Flag = False
FileFlag = False
offset1 = 0
offset2 = 0
if os.path.exists(Filename) == 0:
return "File Doesn't exist", 1
else:
fh = open(Filename,"r")
for line in fh:
if Search in line:
print "Success"
print (line)
Flag = True
offset1 = fh.tell()
#offset1 = int(offset1)
break
else:
fh.close()
return "Could not find String %s"%(Search), 1
#fh.close()
if Flag:
fh = open(Filename,"r")
print(offset1)
fh.seek(offset1)
for line in fh:
if "TestDir1\TestFile1.txt" in line:
print "Success"
print (line)
FileFlag = True
offset2 = fh.tell()
#offset2 = int(offset2)
break
else:
fh.close()
return "Couldn't Find File TestDir1\TestFile1.txt", 1
#fh.close()
if Flag and FileFlag:
fh = open(Filename,"r")
print(offset2)
fh.seek(offset2)
for line in fh:
if "Persistent Handle: True" in line:
print "Success"
return "Success -- Found the strings", 0
else:
fh.close()
return "Failur -- Failed to find 'Persistent Handle: True'", 1
输出:
>>> CheckFile("D:\wireshark.txt","NetBIOS")
('Could not find String NetBIOS', 1)
这是示例文件:
> [SEQ/ACK analysis]
> [This is an ACK to the segment in frame: 104]
> [The RTT to ACK the segment was: 0.043579000 seconds]
> [Bytes in flight: 252]
>NetBIOS Session Service
> Message Type: Session message (0x00)
> Length: 248
>SMB2 (Server Message Block Protocol version 2)
> SMB2 Header
> Server Component: SMB2
> Header Length: 64
> Credit Charge: 1
> Channel Sequence: 0
Reserved: 0000
Command: Create (5)
Credits requested: 1
Flags: 0x00000000