Steven D'Aprano wrote:
On Wed, 11 Nov 2009 17:24:37 -0800, hong zhang wrote:

List,

I have a question of python using echo.

POWER = 14
return_value = os.system('echo 14 >
/sys/class/net/wlan1/device/tx_power')

can assign 14 to tx_power

But
return_value = os.system('echo $POWER >
/sys/class/net/wlan1/device/tx_power')

POWER = 14 doesn't create an environment variable visible to echo. It is a Python variable.

POWER = 14
import os
return_value = os.system('echo $POWER')

return_value
0

You can set environment variables from within Python using os.putenv:

>>> import os
>>> os.putenv('POWER', '14')
>>> return_value = os.system('echo $POWER')
14
>>> return_value
0

Keep in mind that putenv() only affects processes started by Python
after you call putenv.  It does not, for example, affect the shell
process you used to invoke Python:

$ POWER=14
$ python -c 'import os
os.putenv("POWER", "42")
os.system("echo $POWER")'
42
$ echo $POWER
14
$

return_value is 256 not 0. It cannot assign 14 to tx_power.

I don't understand that. Exit status codes on all systems I'm familiar with are limited to 0 through 255. What operating system are you using?

Probably some form of Unix.  The value returned by os.system() is the
exit status shifted left one byte, for example:

>>> os.system("exit 1")
256

Hope this helps,

-- HansM

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to