我在用
subprocess.call(['prog', 'arg'], shell=False)
Run Code Online (Sandbox Code Playgroud)
执行编程并让我自己逃避arg.
现在有时候prog需要来自STDIN的一些输入.在我可以使用的shell中
echo 'some input' | prog arg
Run Code Online (Sandbox Code Playgroud)
用管子.如何在不自动转义arg的情况下使用子进程执行此操作?这甚至可能吗?
或者是唯一的方法
subprocess.call('echo "%s" | prog "%s"' % ('some input', 'arg'), shell=True)
Run Code Online (Sandbox Code Playgroud)
这根本不安全.
proc = subprocess.Popen(['prog', 'arg'], shell=False, stdin=subprocess.PIPE)
out, err = proc.communicate('some input')
Run Code Online (Sandbox Code Playgroud)
这基本上就是call引擎盖下的内容,除了周围的物体让你有机会调用communicate它.(然后返回代码proc.returncode.)
请注意,如果您想要实际获取stdout或stderr,则还需要传递PIPE给构造函数.如上所述,他们都会出现None.
文件.
事后的想法:如果你正在进行大量的外部呼叫,你可能也想让plumbum旋转一下; 它做了一堆运算符重载hackery来提供类似于shell的语法.