2

Python を使用してバックグラウンドで DOS バッチ ファイルを実行する方法は?

たとえば C:\ に test.bat ファイルがあります。
バックグラウンドで Python を使用してこのバット ファイルを実行し、Python コマンド ラインに戻りたいと考えています。

subprocess.call('path\to\test.bat')Pythonコマンドラインから使用してバッチファイルを実行します。Python コマンド ラインと同じウィンドウでバッチ ファイルを実行します。

それでもクリアできない場合/TL.DR-

何が起こっている:

>>>subprocess.call('C:\test.bat')
(Running test.bat. Can't use python in the same window)

私が欲しいもの:

>>>subprocess.call('C:\test.bat')
(New commandline window created in the background where test.bat runs in parallel.)
>>>
4

3 に答える 3

0

のドキュメントはsubprocess.call言う

args で記述されたコマンドを実行します。コマンドが完了するのを待ってから、returncode 属性を返します。

あなたの場合、プログラムを続行する前にコマンドが完了するのを待ちたくないので、subprocess.Popen代わりに次を使用してください。

subprocess.Popen('C:\test.bat')
于 2012-10-09T06:10:17.800 に答える
0

私は使うだろう

subprocess.Popen("test.bat", creationflags=subprocess.CREATE_NEW_CONSOLE)
#subprocess.call("ls")
#etc...

そのため、別のコマンド ウィンドウで「test.bat」を実行して、同じファイル内の他のコマンドを呼び出し続けることができます。

于 2016-04-25T12:20:52.960 に答える