python with echo

Diez B. Roggisch deets at nospam.web.de
Thu Nov 12 11:41:12 CET 2009


hong zhang schrieb:
> 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')
> 
> return_value is 256 not 0. It cannot assign 14 to tx_power.

Because $POWER is an environment-variable, but POWER is a 
python-variable, which isn't magically becoming an environment variable.

There are various ways to achieve what you want, but IMHO you should 
ditch the whole echo-business as it's just needless in python itself:

with open("/sys/class/net/wlan1/device/tx_power", "w") as f:
     f.write("%i" % POWER)

Diez



More information about the Python-list mailing list