子流程中的变量

用户名

有什么方法可以在子过程中传递变量?这没用。

subprocess.check_output(["cat test.txt | grep %s"], shell=True) %(variable)
帕德拉克·坎宁安(Padraic Cunningham)

您可以使用str.format

"cat test.txt | grep {}".format(variable)

或使用旧样式格式将变量直接放在后面。

"cat test.txt | grep %s"%variable

当使用shell = True时,您的列表也是多余的:

subprocess.check_output("cat fable.txt | grep %s" %variable, shell=True)

如果您使用的是不带shell = True的args列表,则可以使用Popen:

from subprocess import Popen,PIPE
p1 = Popen(["cat","text.txt"], stdout=PIPE)
p2 = Popen(["grep", variable], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  
out,err = p2.communicate()
p1.wait()
print(out)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章