运行 来自 Python 的 Stata 并确保没有错误
Run Stata from Python and assure that there was no errors
我知道如何从 Python 开始使用 Stata。我这里有小程序
def dostata(dofile, *params):
## Launch a do-file, given the fullpath to the do-file
## and a list of parameters.
cmd = ["C:\Program Files (x86)\Stata13\StataMP-64.exe", "do", dofile]
for param in params:
cmd.append(param)
a = subprocess.Popen(cmd, shell=True)
path = "C:/My/do/file/dir/"
filename = "try.do"
dostata(path + filename, model, "1", "")
这或多或少起作用了。但这并不能保证 Stata 程序会成功完成。我怎样才能从 Stata 得到一些反馈给 Python 说 "Completed Successfully"?
子进程使用returncode
到return成功(零)或失败(非零)结果底层被调用的进程。
但是,Stata do 文件不完全是可执行文件,而是 运行 作为 batch jobs. For this reason, Stata.exe will always return a success code as it will always run regardless of .do
code output. Therefore, consider the below where Python reads and outputs Stata's log to its console for user to see code result. Possibly even condition Python to scan log file for any Stata error code、r(1)
- r(9999)
,如果存在于日志文件中则强制 Python 给它留言。
import os, subprocess
# CURRENT DIRECTORY
cd = os.path.dirname(os.path.abspath(__file__))
def openlog(filename):
with open(filename, 'r') as txt:
for line in txt:
print(line.strip())
def dostata(dofile, logfile, *params):
## Launch a do-file, given the fullpath to the do-file
## and a list of parameters.
cmd = ["C:\Program Files (x86)\Stata13\StataMP-64.exe", "/b", "do", dofile]
for param in params:
cmd.append(param)
a = subprocess.Popen(cmd, shell=True)
print('STATA OUTPUT:\n')
openlog(os.path.join(cd, logfile))
path = "C:/My/do/file/dir/"
filename = "try.do"
logname = "try.log"
result = dostata(os.path.join(path, filename), logname, "")
我知道如何从 Python 开始使用 Stata。我这里有小程序
def dostata(dofile, *params):
## Launch a do-file, given the fullpath to the do-file
## and a list of parameters.
cmd = ["C:\Program Files (x86)\Stata13\StataMP-64.exe", "do", dofile]
for param in params:
cmd.append(param)
a = subprocess.Popen(cmd, shell=True)
path = "C:/My/do/file/dir/"
filename = "try.do"
dostata(path + filename, model, "1", "")
这或多或少起作用了。但这并不能保证 Stata 程序会成功完成。我怎样才能从 Stata 得到一些反馈给 Python 说 "Completed Successfully"?
子进程使用returncode
到return成功(零)或失败(非零)结果底层被调用的进程。
但是,Stata do 文件不完全是可执行文件,而是 运行 作为 batch jobs. For this reason, Stata.exe will always return a success code as it will always run regardless of .do
code output. Therefore, consider the below where Python reads and outputs Stata's log to its console for user to see code result. Possibly even condition Python to scan log file for any Stata error code、r(1)
- r(9999)
,如果存在于日志文件中则强制 Python 给它留言。
import os, subprocess
# CURRENT DIRECTORY
cd = os.path.dirname(os.path.abspath(__file__))
def openlog(filename):
with open(filename, 'r') as txt:
for line in txt:
print(line.strip())
def dostata(dofile, logfile, *params):
## Launch a do-file, given the fullpath to the do-file
## and a list of parameters.
cmd = ["C:\Program Files (x86)\Stata13\StataMP-64.exe", "/b", "do", dofile]
for param in params:
cmd.append(param)
a = subprocess.Popen(cmd, shell=True)
print('STATA OUTPUT:\n')
openlog(os.path.join(cd, logfile))
path = "C:/My/do/file/dir/"
filename = "try.do"
logname = "try.log"
result = dostata(os.path.join(path, filename), logname, "")