-1

Linuxボックスでcallコマンドを使用してPythonスクリプトから「そのまま」実行しようとしている同等のシェルコマンド(以下に表示)があり、コンパイルエラーが発生します。このコマンドをruntするための呼び出し以外のより良い方法はありますか?どこが間違っているのですか?

from subprocess import call

def main ():

#ssh -p 29418 company.com gerrit query --commit-message --files --current-patch-set status:open project:platform/vendor/company-proprietary/wlan branch:master |grep refs| awk -F ' ' {'print $2'} |tee refspecs.txt
    call (["ssh -p 29418 company.com gerrit query", "--commit-message", "--files", "--current-patch-set", "status:open project:platform/vendor/company-proprietary/wlan branch:master","|grep refs| awk -F ' ' {'print $2'} |tee refspecs.txt")]

if __name__ == '__main__':
    main()
4

4 に答える 4

2

分割する必要があります"ssh -p 29418 company.com gerrit query"(手動で行う、使用しない.split()など)。

現在、への呼び出しはsubprocess.call()その文字列全体を実行しようとしますが、PATHにその名前の名前が含まれていないことは明らかです。

于 2012-12-27T21:14:36.100 に答える
2

かなり深刻なパイプラインを設定しているので、最も簡単な方法は、シェルを使用してコマンドを実行することです。また、check_call問題があったかどうかがわかるように使用を検討してください。そしてもちろん、)]最後])にコンパイルエラーを修正する必要があります:

from subprocess import check_call

def main ():
    check_call("ssh -p 29418 company.com gerrit query --commit-message --files --current-patch-set status:open project:platform/vendor/company-proprietary/wlan branch:master | grep refs | awk -F ' ' {'print $2'} | tee refspecs.txt", shell=True)

if __name__ == '__main__':
    main()
于 2012-12-27T21:18:58.607 に答える
1
from subprocess import check_call

with open("refspecs.txt", "wb") as file:
    check_call("ssh -p 29418 company.com "
        "gerrit query --commit-message --files --current-patch-set "
        "status:open project:platform/vendor/company-proprietary/wlan branch:master |"
        "grep refs |"
        "awk -F ' ' '{print $2}'",
               shell=True,   # need shell due to the pipes
               stdout=file)  # redirect to a file

teestdoutを抑制するために削除しました。

注:コマンドの一部または全体をPythonで実装できます。

于 2012-12-27T21:27:00.587 に答える
0

呼び出しを閉じるために、)と]の順序が間違っていました。

call (["ssh -p 29418 company.com gerrit query", "--commit-message", "--files", "--current-patch-set", "status:open project:platform/vendor/company-proprietary/wlan branch:master","|grep refs| awk -F ' ' {'print $2'} |tee refspecs.txt"])
于 2012-12-27T21:16:30.723 に答える