python - 重定向 stdout 时在 python 上调用子进程时出错

标签 python subprocess

我尝试过滤由 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/

相关文章:

python - 识别 python 日志处理程序类型

python - 在 python 中使用 cv2.calcHist 获取目录中多个图像的颜色直方图

python - 通过名称获取对嵌入式 python 函数的 C 引用?

python - 通过 Python subprocess.Popen() 将转义字符传递给 grep -e 命令

python - 在子进程中加载​​ pickle

python - 绕过不可微分的 tf.argmax

python - 从 Tkinter 中的单选框获取值 - Python

Python:Tkinter 关机、重启和 sleep

python - 如何写入 Python 子进程的标准输入?

python - 如何在所有 xml 文件上调用 .py?