私信  •  关注

Ryan Thompson

Ryan Thompson 最近创建的主题
Ryan Thompson 最近回复了
10 年前
回复了 Ryan Thompson 创建的主题 » 使用sed命令的python[复制]

要回答最初的问题,要重定向输出,只需传递 stdout 论证 subprocess.call :

# 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.call(my_cmd, stdout=outfile)

但正如其他人指出的,外部命令的使用 cat 为了这个目的完全是无关的。