[英]using subprocess module in python to run isql command
I have to run isql command using python. 我必须使用python运行isql命令。
Currently i'm doing it in this way 目前我正在以这种方式
ps = subprocess.Popen("""./isql -I /app/sybase/interfaces_global -S %s -U %s -P %s -D %s -s "|" -w 99999 <<EOF
SET NOCOUNT ON
%s
go
EOF""" %(mdbserver,muserid,mpassword,mdatabase,User_Query),stdout=subprocess.PIPE,shell=True,cwd=sybase_path)
But this method is dependent on the /tmp directory of my server because of the here document, everytime when i run it, it creates a tmp file in the /tmp directory and when the /tmp directory is full the script fails to run the Query onto the database. 但是由于此文档,此方法取决于服务器的/ tmp目录,每次运行它时,它都会在/ tmp目录中创建一个tmp文件,而当/ tmp目录已满时,脚本将无法运行查询到数据库上。
How can i use the same command with shell=False
, So that i can get rid of the here document """ and the temporary file creation. 我如何在
shell=False
使用同一命令,以便摆脱这里的文档“”和临时文件的创建。
this doesn't works 这行不通
ps = subprocess.Popen("./isql","-I","/app/sybase/interfaces_global","-S",mdbserver,"-U",muserid,"-P",mpassword,"-D",mdatabase,"-s","|","-w","99999","\nSET NOCOUNT ON\n",User_Query,"\ngo",stdout=subprocess.PIPE,shell=False,cwd=sybase_path)
You could replace the here-document by setting stdin=PIPE
and providing the input as a string using .communicate()
method as @Hans Then suggested : 您可以通过设置
stdin=PIPE
并使用.communicate .communicate()
方法将输入作为字符串提供为@Hans来替换此处的文档, 然后建议 :
from subprocess import Popen, PIPE
from textwrap import dedent
isql = Popen(['./isql', '-I', '/app/sybase/...',
'-S', mdbserver,
'-U', muserid, ...,
'-w', '99999'], stdin=PIPE, stdout=PIPE, cwd=sybase_path)
output = isql.communicate(dedent("""\
SET NOCOUNT ON
{}
go
""".format(User_Query)))[0]
Check out the subprocess communicate()
command. 签出子流程
communicate()
命令。 You can use it to send isql commands to the interpreter. 您可以使用它将isql命令发送到解释器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.