【发布时间】:2013-07-08 03:58:13
【问题描述】:
我正在将我的以下 bash 脚本迁移到 python
Bash 代码
sqlplus -S User/psw@inst << EOF
whenever sqlerror exit sql.sqlcode;
set echo off
set heading off
prompt *****************************
prompt *** Running Script 1***
@/my/location/Script1
prompt *****************************
prompt *** Running Script 2***
@/my/location/Script2
exit;
EOF
Python 代码
(username, password, host) = ("user","psw","isntance")
conn_string = " %s/%s@%s "% (username,password,host)
#for script1
session = Popen(['sqlplus','-S', conn_string], stdin=PIPE, stdout=PIPE, stderr=PIPE)
sql_file = '%s/%s' % ('/my/folder', 'Script1.sql')
with open(sql_file) as f:
stdout, stderr = session.communicate(f.read())
print stdout
print stderr
#for script2
session = Popen(['sqlplus','-S', conn_string], stdin=PIPE, stdout=PIPE, stderr=PIPE)
sql_file = '%s/%s' % ('/my/folder', 'Script2.sql')
with open(sql_file) as f:
stdout, stderr = session.communicate(f.read())
print stdout
print stderr
我有两个问题,
1) 有没有办法在不打开另一个脚本的情况下运行这两个脚本
Popen 的实例
2)如何实现prompt 功能
在python 代码中?
【问题讨论】: