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

Charlie Reddington charlie.reddington at gmail.com
Wed Jun 24 01:24:47 CEST 2009


On Jun 23, 2009, at 4:50 PM, Alan Gauld wrote:

>
> "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  
> operator(%)
> 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 that
> 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.
>
> HTH,
>

Thanks for all the replies, I'll definitely look into it all.

Charlie


> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list