Python - 让 os.popen 匹配一个字符串?

Python - getting os.popen to match a string?

我正在尝试 运行 对 .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()