に私の古いPythonスクリプトでは、私は、WindowsのCMDコマンドの結果を表示するには、次のコードを使用subprocess.Popenを使用する方法:内蔵したコマンドのWindows
print(os.popen("dir c:\\").read())
のpython 2.7文書はos.popen
言ったようには廃止され、subprocess
が推奨されます。私はドキュメントは、次のとおりです。
result = subprocess.Popen("dir c:\\").stdout
をそして私は、エラーメッセージました:
WindowsError: [Error 2] The system cannot find the file specified
はあなたが私にsubprocess
モジュールを使用するための正しい方法を教えてもらえますか?
import subprocess
result = subprocess.Popen("dir c:", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output,error = result.communicate()
print (output)
More info on subprocess module.
Windows上の 'dir'はシェルに組み込まれているため、スタンドアロンの実行ファイルではありません。http://stackoverflow.com/questions/20330385/cannot-find-the-file-specified-when-usingを参照してください。 -subprocess-calldir-shell-true-in – metatoaster
@metatoasterありがとうございました。私が記事を読んだ後、私の理解は、組み込みシェルコマンドは 'サブプロセス 'によって呼び出すことができないということです。このシナリオでの 'os.popen'は '時代遅れ'ではありませんか? –