Python -获取os.Popen匹配字符串?



我试图在.doc文件上运行grep以获取字符串并将其与宏的证据相匹配,以便检测它。

我:

filename = raw_input("file name: ") <--- using malicious.doc
s1 = os.popen("grep '/vbaProject|/vbaData' " + filename).read()
s2 = 'Binary file ' + filename + ' matches'
if s1 == s2:
print("true")
else:
print("false")

如果我运行:

if s1 == s2:
print("true")
else:
print("false")
print(s1)
print(s2)

得到以下输出:

false
Binary file malicious.doc matches
Binary file malicious.doc matches

文本匹配,我甚至尝试做s1 = str(s1) and s2 = str(s2),仍然得到假。

提前感谢。

通过您的注释,您在结果中获得了一个额外的换行符。

我通常建议在比较结果之前从空白中删除结果。在你的情况下,我会这样做:

s1 = os.popen("grep '/vbaProject|/vbaData' " + filename).read().strip()

最新更新