如何在 Python 中用子进程重定向输出?

我在命令行中的操作:

cat file1 file2 file3 > myfile

我想对巨蟒做什么:

import subprocess, shlex
my_cmd = 'cat file1 file2 file3 > myfile'
args = shlex.split(my_cmd)
subprocess.call(args) # spits the output in the window i call my python program
131060 次浏览

更新: 不鼓励使用 os.system,尽管在 Python 3中仍然可用。


使用 os.system:

os.system(my_cmd)

如果你真的想要使用子流程,这里有一个解决方案(主要是从子流程的文档中提取的) :

p = subprocess.Popen(my_cmd, shell=True)
os.waitpid(p.pid, 0)

OTOH,您可以完全避免系统调用:

import shutil


with open('myfile', 'w') as outfile:
for infile in ('file1', 'file2', 'file3'):
shutil.copyfileobj(open(infile), outfile)

@PoltoS I want to join some files and then process the resulting file. I thought using cat was the easiest alternative. Is there a better/pythonic way to do it?

当然:

with open('myfile', 'w') as outfile:
for infilename in ['file1', 'file2', 'file3']:
with open(infilename) as infile:
outfile.write(infile.read())

Python 3.5 + 中,要重定向输出,只需将 stdout参数的打开文件句柄传递给 subprocess.run:

# Use a list of args instead of a string
input_files = ['file1', 'file2', 'file3']
my_cmd = ['cat'] + input_files
with open('myfile', "w") as outfile:
subprocess.run(my_cmd, stdout=outfile)

正如其他人指出的,为此目的使用像 cat这样的外部命令完全是无关紧要的。

One interesting case would be to update a file by appending similar file to it. Then one would not have to create a new file in the process. It is particularly useful in the case where a large file need to be appended. Here is one possibility using teminal command line directly from python.

import subprocess32 as sub


with open("A.csv","a") as f:
f.flush()
sub.Popen(["cat","temp.csv"],stdout=f)
size = 'ffprobe -v error -show_entries format=size -of default=noprint_wrappers=1:nokey=1 dump.mp4 > file'
proc = subprocess.Popen(shlex.split(size), shell=True)
time.sleep(1)
proc.terminate() #proc.kill() modify it by a suggestion
size = ""
with open('file', 'r') as infile:
for line in infile.readlines():
size += line.strip()


print(size)
os.remove('file')

使用 子流程时,必须终止进程。这是一个例子,如果你不终止这个进程,文件将是空的,你什么也读不出来。它可以在 窗户上运行。我不能确定它能在 Unix 上运行。

It will work if your args will look like ['sh', '-c', 'cat file1 file2 file3 > myfile'] it will mean that output of cat won't pass Python and spawn in shell instead (insted of sh -c you can use bash -c)