我正在嘗試在 python 中執行一個 shell 腳本,但我沒有得到預期的結果
腳本檔案
#!/bin/bash
file1=$1
file2=$2
cat $file1 $file2
Python:
print(sp.run(/path/script.sh " text1.txt text2.txt", shell=True, check=True, text=True, capture_output=True))
如果我在終端上運行腳本結果是正確的,我就會加入這兩個檔案。但是如果我運行 python 代碼,它似乎什么都不做。
uj5u.com熱心網友回復:
以下代碼段應該適合您:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/388383.html
上一篇:sass的基本語法整理
下一篇:Bashshell選項-r
