35

これが重複していないことを願っています。

subprocess.Popen()別のコンソールでスクリプトを開くために使用しようとしています。shell=Trueパラメータを設定しようとしましたが、うまくいきませんでした。

64 ビットの Windows 7 で 32 ビットの Python 2.7 を使用しています。

4

3 に答える 3

9
from subprocess import *

c = 'dir' #Windows

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()

使用しない場合は、コマンド文字列の代わりにリストをshell=True指定する必要があります。例:Popen()

c = ['ls', '-l'] #Linux

次に、シェルなしで開きます。

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
print handle.stdout.read()
handle.flush()

これは、Python からサブプロセスを呼び出す最も手動で柔軟な方法です。出力だけが必要な場合は、次のようにします。

from subproccess import check_output
print check_output('dir')

新しいコンソール GUI ウィンドウを開いて X を実行するには:

import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)
于 2013-04-09T10:44:04.847 に答える