簡體   English   中英

通過子進程運行腳本文件

[英]Running script file through subprocess

我正在嘗試通過 Python 的子進程模塊運行 Linux 腳本。 下面是子進程命令:

result = subprocess.run(['/dir/scripts/0_texts.sh'], shell=True)
print(result)

這是0_texts.sh腳本文件:

cd /dir/TXTs

pylanguagetool text_0.txt > comments_0.txt

subprocess 命令執行腳本文件,在正確的目錄中寫入一個新的comments_0.txt 但是,執行時出現錯誤。 comments_0.txt出現“input file is required”的錯誤,subprocess 結果返回returncode=2 當我直接在終端中運行pylanguagetool text_0.txt > comments_0.txt時,命令正確執行, comments_0.txt使用正確的輸入文件text_0.txt

對我所缺少的有什么建議嗎?

這里有一些歧義,因為每次調用0_texts.sh時運行哪個 shell 並不明顯,以及它是否具有您期望的環境變量值,如PATH ,這可能會導致pylanguagetool在您調用時運行不同的副本它在命令行。

首先,我建議刪除 subprocess.run 中的subprocess.run shell=True選項,它只涉及另一個可能不同的 shell 。 接下來,我會將subprocess.run(['/dir/scripts/0_texts.sh'])更改為 subprocess.run subprocess.run(['bash', '/dir/scripts/0_texts.sh']) (或您想要的任何一個 shell運行,可能是bashdash )以消除歧義源。 最后,您可以嘗試在腳本中使用type pylanguagetool ,使用其完整路徑調用pylanguagetool ,或從您的終端調用bash /dir/scripts/0_texts.sh以進一步調試情況。

一個更大的問題是,pyLanguageTool 是一個 Python 庫,因此您幾乎可以肯定直接從原始 Python 腳本調用它的函數會更好,而不是使用 shell 腳本作為中介。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM