如何将子进程调用传输到文本文件?

时间:2023-02-10 22:09:06
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.

现在我有一个运行的脚本。当我运行它,它碰到这条线,它开始打印东西,因为运行。sh里面有指纹。

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

如何将其传输到文本文件?(如果可能的话,也可以打印)

2 个解决方案

#1


98  

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

如果要将输出写入文件,可以使用subprocess.call的stdout参数。

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.

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

You need to open a file with something like open and pass the object or file descriptor integer to 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.

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

As marcog mentions 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的相同位置。上面提到的任何值都可以工作,您可以重定向到不同的位置。

#2


16  

The options for popen can be used in call

popen的选项可以在调用中使用

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手册页面上有许多可爱的、有用的信息。

#1


98  

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

如果要将输出写入文件,可以使用subprocess.call的stdout参数。

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.

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

You need to open a file with something like open and pass the object or file descriptor integer to 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.

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

As marcog mentions 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的相同位置。上面提到的任何值都可以工作,您可以重定向到不同的位置。

#2


16  

The options for popen can be used in call

popen的选项可以在调用中使用

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手册页面上有许多可爱的、有用的信息。