[Tutor] Trouble with passing commands / variables to os.system()

Alan Gauld alan.gauld at btinternet.com
Tue Jun 23 23:50:30 CEST 2009

"Charlie Reddington" <charlie.reddington at gmail.com> wrote

> Your code works as expected! Can you tell me what your code is doing
> different than mine?

os.system needs the command to be a string so you have to build
up the string by passing in your variables using the string format 
or building it bit by bit outside the call to system.

Beware that the return value from system is just the exit code which is not
very useful, hencehthe recommendation to oook at subprocess...

>> for host in servers:
>>     os.system("ssh %s@%s -i %s %s" %(username, host, private_key,
>> command)

Also while this might seem a good approach to start you will likely find 
most of the things you are doing via sysyem will be possible directly
from Python which will be more efficient in resources.

Take a look at the Using the OS topic in my tutorioal for just a
few examples of the kinds of things you can do directly - as well
as how to use subprocess.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list