【发布时间】:2011-06-25 07:25:19
【问题描述】:
我在命令行中做了什么:
cat file1 file2 file3 > myfile
我想用python做什么:
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
【问题讨论】:
-
在子进程中执行这样的命令不会给你任何输出。也许你想在没有 > myfile 将输出从 cat file1 file2 file3 重定向到 python 的情况下运行它?
-
@PoltoS 我想加入一些文件,然后处理生成的文件。我认为使用 cat 是最简单的选择。有没有更好的/pythonic 方式来做到这一点?
-
os.sendfile()-based 解决方案是可能的,请参阅Reproduce the unix cat command in python -
我认为输出重定向('>' 或 '>>')在 subprocess.Popen 中不起作用(至少在 Python 2.7 中)(在 shell=True 模式下)在这个例子中,如其他人指出,您可以通过不使用重定向来解决此问题,但在其他情况下,重定向很有用。如果 subprocess.Popen 不支持重定向或管道,则应记录(和/或 os.system() 在修复之前不应弃用)
标签: python subprocess