1. 程式人生 > >python:執行外部程式(以stata程式為例)

python:執行外部程式(以stata程式為例)

桌面存在一個stata的do檔案dofile.do:

clear all
sysuse auto
regress price weight length
python程式碼為:
#note:需要用到subprocess模組
import os
# 改變路徑到stata程式所在目錄
os.chdir("C:\\Program Files (x86)\\Stata14")
import subprocess
# stata程式名稱為"StataMP-64.exe"    
cmd = ["StataMP-64", "do", "C:\\Users\\zk\\Desktop\\dofile.do"]
subprocess.call(cmd) 

可參看:https://stackoverflow.com/questions/21263668/run-stata-do-file-from-python