【问题标题】:subprocess.run() doesn't return the expected output [closed]subprocess.run()不返回预期的输出[关闭]
【发布时间】:2022-01-22 17:13:47
【问题描述】:

我正在尝试在 python 中执行一个 shell 脚本,但我没有得到预期的结果

脚本.sh

#!/bin/bash
file1=$1
file2=$2
cat $file1 $file2

蟒蛇:

print(sp.run(/path/script.sh + " text1.txt text2.txt", shell=True, check=True, text=True, capture_output=True))

如果我在终端上运行脚本结果是正确的,我会加入这两个文件。 但是如果我运行 python 代码,它似乎什么都不做。

【问题讨论】:

  • /path/script.sh 不是有效的 Python 文字。

标签: python shell subprocess


【解决方案1】:

以下 sn-p 应该适合你:

import subprocess as sp

file_name = "path/to/script.sh"
file1 = "path/to/file1"
file2 = "path/to/file2"

output = sp.run([file_name, file1, file2], check=True, text=True, capture_output=True)
print(output.stdout)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多