shell - Python 2.4: Get the output of system commands into a non-quoted string -
i want write output of psql-command integer variable can use "if x larger 0" statement in python, cannot value only.
my environment limited. cannot import psycopg(2) , cannot update have work given commands of python 2.4.
see following:
>>> os.popen('psql -d database -u datareader -t -a -c "select count(*) mails"').read() '151\n'
or
>>> commands.getoutput('psql -d database -u datareader -t -a -c "select count(*) mails"') '151'
both outputs @ least singlequoted strings , don't know how use them integers.
another way older os.system
:
>>> os.system('psql -u database -d datareader -t -a -c "select count(*) mails"') 151 0
no quotes, output of command exit code , commands output. putting variable puts exit code in, not command output.
the latter 1 go workaround writing output appended file. however, avoid.
the output external command always going string. convert integer manually:
output = commands.getoutput('psql -d database -u datareader -t -a -c "select count(*) mails"') count = int(output)
Comments
Post a Comment