我试图过滤由python脚本中的函数生成的文件:
out = subprocess.check_output(["sed","-n","'s/pattern/&/p'",oldFile,">",newFile])
但是,我的命令有以下错误:
returned non-zero exit status 1
怎么了?
最佳答案
如devnull所述,>
由shell解释。因为它是better to avoid using shell=True
,所以改用stdout
参数:
import subprocess
with open(newFile, 'w') as newFile:
subprocess.check_call(
["sed", "-n", "s/S/&/p", oldFile], stdout=newFile)