Linux 如何通过管道将子进程调用传递给文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4856583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:15:37  来源:igfitidea点击:

How do I pipe a subprocess call to a text file?

pythonlinuxshellunixsubprocess

提问by TIMEX

subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml",  "/tmp/video_xml"])

RIght now I have a script that I run. When I run it and it hits this line, it starts printing stuff because run.sh has prints in it.

现在我有一个我运行的脚本。当我运行它并到达这一行时,它开始打印内容,因为 run.sh 中有打印内容。

How do I pipe this to a text file also? (And also print, if possible)

我如何将它也通过管道传输到文本文件?(如果可能,还可以打印)

采纳答案by Skurmedel

If you want to write the output to a file you can use the stdout-argument of subprocess.call.

如果你想将输出写入到一个文件,你可以使用标准输出的-argument subprocess.call

It takes None, subprocess.PIPE, a file object or a file descriptor. The first is the default, stdout is inherited from the parent (your script). The second allows you to pipe from one command/process to another. The third and fourth are what you want, to have the output written to a file.

它需要None, subprocess.PIPE, 一个文件对象或一个文件描述符。第一个是默认值,stdout 继承自父级(您的脚本)。第二个允许您从一个命令/进程管道到另一个。第三个和第四个是您想要的,将输出写入文件。

You need to open a file with something like openand pass the object or file descriptor integer to call:

您需要使用类似的内容打开文件open并将对象或文件描述符整数传递给call

f = open("blah.txt", "w")
subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml",  "/tmp/video_xml"], stdout=f)

I'm guessing any valid file-like object would work, like a socket (gasp :)), but I've never tried.

我猜任何有效的类似文件的对象都可以工作,比如套接字(喘气:)),但我从未尝试过。

As marcogmentions in the comments you might want to redirect stderr as well, you can redirect this to the same location as stdout with stderr=subprocess.STDOUT. Any of the above mentioned values works as well, you can redirect to different places.

正如marcog在评论中提到的,您可能还想重定向 stderr,您可以将其重定向到与 stdout 相同的位置stderr=subprocess.STDOUT。上面提到的任何值也都有效,您可以重定向到不同的地方。

回答by ocodo

The options for popencan be used in call

的选项popen可用于call

args, 
bufsize=0, 
executable=None, 
stdin=None, 
stdout=None, 
stderr=None, 
preexec_fn=None, 
close_fds=False, 
shell=False, 
cwd=None, 
env=None, 
universal_newlines=False, 
startupinfo=None, 
creationflags=0

So...

所以...

subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml",  "/tmp/video_xml"], stdout=myoutput)

Then you can do what you want with myoutput(which would need to be a file btw).

然后你可以做你想做的事myoutput(顺便说一句,这需要是一个文件)。

Also, you can do something closer to a piped output like this.

此外,您可以做一些更接近于像这样的管道输出的事情。

dmesg | grep hda

would be:

将是:

p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

There's plenty of lovely, useful info on the python manual page.

python 手册页上有很多可爱、有用的信息。