Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: call from pthon to shell

Reply
Thread Tools

Re: call from pthon to shell

 
 
Andrew Robinson
Guest
Posts: n/a
 
      02-12-2013
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.





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: call from pthon to shell Jason Friedman Python 0 02-17-2013 07:00 AM
call from pthon to shell Bqsj Sjbq Python 0 02-12-2013 05:38 AM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57