Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38737

Re: call from pthon to shell

Date 2013-02-12 01:00 +0000
From Andrew Robinson <andrew3@r3dsolutions.com>
Subject Re: call from pthon to shell
References <1360647500.45284.YahooMailNeo@web160602.mail.bf1.yahoo.com>
Newsgroups comp.lang.python
Message-ID <mailman.1693.1360659774.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 02/12/2013 05:38 AM, Bqsj Sjbq wrote:
> >>> import os
> >>> os.system("i=3")
> 0
> >>> os.system("echo $i")
>
> 0
>
> why i can not get the value of i?
>
>
First:
os.system is only defined to give the return value (exit code) of the 
sub-process.

However, one way to get the output of shell commands is to use subprocess.

import subprocess
x = subprocess.check_output( [ "echo", "3,5,7" ] )

However, bash built-ins are not executables; nor is shell expansion 
performed; so you will actually need to do something like:
x=subprocess.check_output( [ "bash", "-c", "i=3; echo $i" ] )
 >>> x
 >>> '3\n'

To get the result you're interested in.
There may be better ways to get the result you want.... but hopefully 
you understand the problem better.

:)


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: call  from pthon  to shell Andrew Robinson <andrew3@r3dsolutions.com> - 2013-02-12 01:00 +0000

csiph-web