2016-12-06 10 views
-1

私は呼びたいコマンドは次のようになりますファイル入力でコマンドを呼び出す失敗:subprocess.callは

のMycmd ARG1 ARG2 ARG3 < infile.ext >>

outfile.ext

infile.extoutfile.extはログファイルのようなものですが、mycmdはそのプロセスを実行するために読み込みます。

Pythonコードは次のとおりです。

from subprocess import call 

impArgs = "%s %s %s < %s >> %s" % (arg1, arg2, arg3, impFilePath, rptFilePath) 
impResult = call(["mycmd ", impArgs]) 

私は何のエラーが、コマンドが呼び出されていないません、3のimpResultを取得します。これをどうやって解決するのですか?

+0

負帰還プロバイダは建設的な提案にどのような問題の改善するのではなく、単に要求を提供することができれば、私はそれをお願い申し上げます閉鎖 – amphibient

答えて

0

むしろより

impArgs = "%s %s %s < %s >> %s" % (arg1, arg2, arg3, impFilePath, rptFilePath) 
impResult = call(["mycmd ", impArgs]) 

Iは

impCmd = "mycmd %s %s %s < %s >> %s" % (arg1, arg2, arg3, impFilePath, rptFilePath) 
impResult = call(impCmd, shell = True) 

shell = Trueを使用する問題を修正ものであり、結果コードが0であり、コマンドが実行されました。

1

シェルのリダイレクト機能を使用しています。デフォルトではPopenがプロセスを起動するだけです。基本的なケースではシェルはまったく必要ないのでシェルを使用しません。

shell=Trueを使用し、wholeコマンドをstringとして渡します。

サブプロセス輸入コール

impArgs = "mycmd %s %s %s < %s >> %s" % (arg1, arg2, arg3, impFilePath, rptFilePath) 
impResult = call(impArgs, shell=True) 

それともshown in docsとして配管の機能を使用してから:

with open(impFilePath) as src, open(rptFilePath) as dst: 
    call(['mycmd', arg1, arg2. arg3], stdin=src, stdout=dst) 
+0

その間にそれを考え出した。下の私の答えを見てください – amphibient

関連する問題