0

I have to run isql command using python.

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.

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.

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)

2 Answers 2

3

You could replace the here-document by setting stdin=PIPE and providing the input as a string using .communicate() method as @Hans Then suggested:

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]
Sign up to request clarification or add additional context in comments.

Comments

1

Check out the subprocess communicate() command. You can use it to send isql commands to the interpreter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.