Re: call from pthon to shell

Discussion in 'Python' started by Andrew Robinson, Feb 12, 2013.

  1. 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.

    :)
     
    Andrew Robinson, Feb 12, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    5,887
    Jonathan Bromley
    Feb 18, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    630
    Christian Heimes
    Feb 27, 2008
  3. Gerardo Herzig
    Replies:
    1
    Views:
    1,139
    Philipp Pagel
    Feb 27, 2008
  4. Bqsj Sjbq

    call from pthon to shell

    Bqsj Sjbq, Feb 12, 2013, in forum: Python
    Replies:
    0
    Views:
    93
    Bqsj Sjbq
    Feb 12, 2013
  5. Jason Friedman

    Re: call from pthon to shell

    Jason Friedman, Feb 17, 2013, in forum: Python
    Replies:
    0
    Views:
    103
    Jason Friedman
    Feb 17, 2013
Loading...

Share This Page