我尝试过滤由 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)
关于python - 重定向 stdout 时在 python 上调用子进程时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22663193/