我在命令行中做的事情:
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
发布于 2014-07-07 03:47:58
一个有趣的例子是通过将类似的文件附加到文件来更新文件。这样就不需要在这个过程中创建新文件了。在需要追加大文件的情况下,它特别有用。以下是直接从python使用teminal命令行的一种可能性。
import subprocess32 as sub
with open("A.csv","a") as f:
f.flush()
sub.Popen(["cat","temp.csv"],stdout=f)
发布于 2022-01-31 13:36:54
如果你的args
看起来像['sh', '-c', 'cat file1 file2 file3 > myfile']
,这将意味着cat
的输出不会通过Python,而是在shell中产生( sh -c
的实例,你可以使用bash -c
)
https://stackoverflow.com/questions/4965159
复制相似问题