の代わりにSTDIN/STDOUT経由でデータをパイプすることを提案しています。それで、特別なシェルの重要な文字をエスケープし、最大のコマンドラインの長さを超えることを心配する必要はありません。
通常、CL引数ベースのスクリプトとしてあなたがapp.py
のようなものかもしれません:
import sys
if __name__ == "__main__": # ensure the script is run directly
if len(sys.argv) > 1: # if at least one CL argument was provided
print("ARG_DATA: {}".format(sys.argv[1])) # print it out...
else:
print("usage: python {} ARG_DATA".format(__file__))
をそれは明確に引数が渡されることを想定し、別のスクリプトから渡された場合、それはそれをプリントアウトし、caller.py
を言う:
import subprocess
out = subprocess.check_output(["python", "app.py", "foo bar"]) # pass foo bar to the app
print(out.rstrip()) # print out the response
# ARG_DATA: foo bar
しかし、もっと複雑なものを渡したい場合は、dict
としましょう。 dict
は階層構造であるため、1行に表示する方法が必要です。あなたはこのような何かにあなたのcaller.py
セットを持っているかもしれないので、ぴったり合う、しかしのは、基本的なJSONに固執せますフォーマットがたくさんあります:
import json
import subprocess
data = { # our complex data
"user": {
"first_name": "foo",
"last_name": "bar",
}
}
serialized = json.dumps(data) # serialize it to JSON
out = subprocess.check_output(["python", "app.py", serialized]) # pass the serialized data
print(out.rstrip()) # print out the response
# ARG_DATA: {"user": {"first_name": "foo", "last_name": "bar"}}
今あなたがいるという事実を認識するために、あなたのapp.py
を変更した場合再びあなたが得るでしょう、あなたのcaller.py
を実行する場合は、
import json
import sys
if __name__ == "__main__": # ensure the script is run directly
if len(sys.argv) > 1:
data = json.loads(sys.argv[1]) # parse the JSON from the first argument
print("First name: {}".format(data["user"]["first_name"]))
print("Last name: {}".format(data["user"]["last_name"]))
else:
print("usage: python {} JSON".format(__file__))
:それはあなたがその構造にアクセスするためのPython dict
に戻ってそれをデシリアライズすることができます引数としてJSONを受けています
First name: foo
Last name: bar
これは非常に面倒なことですが、JSONはCLにとって非常に親切ではありません(舞台裏では、Pythonはエスケープして動作させています)。 JSONはこのように渡すことができます。 STDIN/STDOUTバッファを使用して、プロセス間で複雑なデータを渡す方がはるかに優れています。これを行うには、app.py
を修正して、そのSTDINへの入力を待ち、caller.py
にシリアル化されたデータを送信する必要があります。
import json
if __name__ == "__main__": # ensure the script is run directly
try:
arg = raw_input() # get input from STDIN (Python 2.x)
except NameError:
arg = input() # get input from STDIN (Python 3.x)
data = json.loads(arg) # parse the JSON from the first argument
print("First name: {}".format(data["user"]["first_name"])) # print to STDOUT
print("Last name: {}".format(data["user"]["last_name"])) # print to STDOUT
とcaller.py
:
import json
import subprocess
data = { # our complex data
"user": {
"first_name": "foo",
"last_name": "bar",
}
}
# start the process and pipe its STDIN and STDOUT to this process handle:
proc = subprocess.Popen(["python", "app.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
serialized = json.dumps(data) # serialize data to JSON
out, err = proc.communicate(serialized) # send the serialized data to proc's STDIN
print(out.rstrip()) # print what was returned on STDOUT
、あなたはあなたが得るもう一度caller.py
を呼び出す場合:
First name: foo
Last name: bar
しかし、この時間に制限はありませんので、app.py
はのような単純なことができあなたがapp.py
に渡しているデータサイズであり、シェルエスケープ中に特定のフォーマットがうまくいかないと心配する必要はありません。 tc。「チャネル」を開いたままにしておき、両方のプロセスが双方向で通信できるようにすることもできます(例:this answer)。
まず、execfileでこれを行う理由を説明してください。 –
ドキュメントについて:https://docs.python.org/2/library/functions.html#execfile 「グローバル」な「ローカル」パラメータがあります。 – Benjamin
入力を読み取るスクリプトを開発しています。いくつかのプロセスを実行し、引数として処理結果を持つ別のpythonスクリプトを呼び出します。私のケースでは動作しなかったos.system、subprocess.popenとsubprocess.callを使用しようとしました – surya