외부파일을 실행하는 방법은 몇가지가 있다.
1. os.system – 실행파일과 파라미터 전달 가능
외부 명령어가 아닌 cmd.exe(도스창)을 실행하여 실행파일을 구동합니다.
종료시까지 메모리에 상주합니다.
ex) 현재경로 + 실행을 원하는 파일명
os.system(os.getcwd()+"\copy.bat")
2. stream = os.popen – 실행파일과 파라미터 전달 가능
os.system과 같이 cmd.exe를 통해 실행되나, 파일같은 object를 써서 프로세스의 I/O에 접근할 수 있습니다.
종료시까지 메모리에 상주합니다.
def RunNotepad():
os.popen("c:\windows\system32\\notepad.exe")
RunNotepad()
3. subprocess 모듈의 call
Popen클래스와 비슷하지만 프로세스가 끝난 후에 값을 return합니다.
cmd.exe를 통하지 않고 바로 실행하지만, 메모리는 실행시킨 Python 귀속하에 있다.
4. ShellExecuteA (윈도우 API)
cmd.exe를 통하지 않고 실행되며, 메모리 상으로도 Python 귀속하지 않는다.
윈도우 API이므로, ctypes 모듈을 통해 사용해야 한다.
예시문
import os
import subprocess
import thread
def RunControl():
os.system(“c:\windows\system32\\control.exe”)
def RunNotepad():
os.popen(“c:\windows\system32\\notepad.exe”)
thread.start_new_thread(RunControl())
thread.start_new_thread(RunNotepad())
subprocess.call(“c:\windows\system32\\control.exe”)
import ctypes
ctypes.windll.shell32.ShellExecuteA(0,’open’.”c:\windows\system32\\control.exe”.None,None,1)
ctypes.windll.shell32.ShellExecuteA(0,’open’.”c:\windows\system32\\notepadl.exe”.None,None,1)
출처 : http://edu.popcornware.net/pop-%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EC%99%B8%EB%B6%80%ED%8C%8C%EC%9D%BC-%EC%8B%A4%ED%96%89/
'python' 카테고리의 다른 글
Django test example (0) | 2019.07.19 |
---|---|
Django install guide (link) (0) | 2019.07.19 |
python3 version 선택 (0) | 2019.07.19 |
virtual env(venv) install in ubuntu 14 (0) | 2019.07.19 |
코루틴 link (0) | 2018.08.27 |