본문 바로가기
파이썬

파이썬 다른 콘솔에서 subprocess.Popen

by º기록 2021. 1. 22.
반응형

나는 이것이 중복되지 않기를 바랍니다.

별도의 콘솔에서 스크립트를 열기 위해 subprocess.Popen () 을 사용하려고합니다. 나는 shell = True 매개 변수를 설정하려고했지만 그것은 트릭을하지 않았습니다.

64 비트 Windows 7에서 32 비트 Python 2.7을 사용합니다.

 

해결 방법

 

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')
import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)

 

참조 페이지 https://stackoverflow.com/questions/15899798

 

 

반응형

댓글