Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38737 > unrolled thread
| Started by | Andrew Robinson <andrew3@r3dsolutions.com> |
|---|---|
| First post | 2013-02-12 01:00 +0000 |
| Last post | 2013-02-12 01:00 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: call from pthon to shell Andrew Robinson <andrew3@r3dsolutions.com> - 2013-02-12 01:00 +0000
| From | Andrew Robinson <andrew3@r3dsolutions.com> |
|---|---|
| Date | 2013-02-12 01:00 +0000 |
| Subject | Re: call from pthon to shell |
| Message-ID | <mailman.1693.1360659774.2939.python-list@python.org> |
[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 top | Article view | comp.lang.python
csiph-web