現在、Windows 764ビットマシン(Linux 32ビット、Ubuntu 12.04でも開発中)でPython 2.7.3を使用していますが、Pythonがコマンドプロンプト/ターミナルと正常に通信できるようにするのに奇妙な問題があります。私のコードは次のようになります:
import subprocess, logging, platform, ctypes
class someClass (object):
def runTerminalCommand:
try:
terminalOrCmdLineCommand = self.toString()
process = subprocess.Popen(terminalOrCmdLineCommand, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output = process.stdout.readlines()
if len(output) < 1:
logging.exception("{} error : {}".format(self.cmd,process.stderr.readlines()))
raise ConfigError("cmd issue : {}".format(self.cmd))
return output
except ValueError as err:
raise err
except Exception as e:
logging.exception("Unexpected error : " + e.message)
raise ConfigError("unexpected error")
これで、self.toString()の戻り値を手動で入力すると正しく処理されることがわかったので、これをサブプロセスを介してコマンドラインに送信する方法の問題に限定します。ドキュメントを読んだところ、subprocess.check_call()でエラーが発生しても何も返されないことがわかったので、.Popen()を使用しています。
私が得る例外は、
[date & time] ERROR: Unexpected error :
Traceback (most recent call last):
File "C:\[...]"
raise ConfigError("cmd issue : {}".format(self.cmd))
"ConfigError: cmd issue : [the list self.cmd printed out]"
私がやろうとしているのは、コマンドを実行し、入力を読み戻すことです。しかし、実行したい呼び出しを自動化できないようです。:(
何かご意見は?(私が省略した必要な詳細があれば教えてください)
よろしくお願いします。