import os
dictionaryfile = "/root/john.txt"
pgpencryptedfile = "helloworld.txt.gpg"
array = open(dictionaryfile).readlines()
for x in array:
x = x.rstrip('\n')
newstring = "echo " + x + " | gpg --passphrase-fd 0 " + pgpencryptedfile
os.popen(newstring)
我需要在for循环中创建一些东西来读取gpg的输出。当gpg输出这个字符串gpg: WARNING: message was not integrity protected
时,我需要关闭循环并打印成功!
我怎么能做到这一点?背后的原因是什么?
谢谢大家!
然后使用:
^{pr2}$如果“gpg:WARNING:”消息实际上在
stderr
上(我怀疑是这样),请将subprocess.Popen
行改为:..以及从}的for循环,如下所示:
stdout
到{使用subprocess.check_output调用
gpg
,并根据其输出中断循环。在如下所示(未经测试,因为我对
gpg
一无所知):您可以使用subprocess模块,该模块允许您使用:
(有关如何使用参数,请参阅Python Documentation。)
这很好,因为你可以很容易地读入你调用的任何程序的退出代码。在
例如,如果将“newstring”更改为:
^{pr2}$如果有匹配项,grep将返回0;如果找到不匹配项,则返回1。(Source)
grep的退出代码将从subprocess.call()函数,您可以轻松地将其存储在变量中并使用if语句。在
编辑:正如马修·亚当斯在下面提到的,你也可以阅读gpg本身的退出代码。在
相关问题 更多 >
编程相关推荐