Using os.system() and string concatenation

Steven Bethard steven.bethard at gmail.com
Tue Oct 12 17:10:43 EDT 2004


Wayne Witzel III <wwitzel3 <at> gmail.com> writes:

> Ok, here is what I had.
> 
> os.system("/usr/sbin/useradd"+ " -d " + user[0] + " -s " + user[1] + "
> " + user[3])
> 
> when I change os.system() to print .. it outputs just fine and I can
> copy and paste the results and they execute. Just using os.system()
> they do not execute unless I place the string in cmd variable first.
> 
> cmd = "/usr/sbin/useradd"+ " -d " + user[0] + " -s " + user[1] + " " + user
[3]
> os.system(cmd)
> 
> This works fine.

Ahh, much clearer, thanks!  Unfortunately, now that I understand your problem, 
I have no idea why this would be true... =(  Can anyone else reproduce the 
error here?  I can't:

>>> import os
>>> os.system("/bin/echo" + " a" + " set" + " of" + " words")
a set of words
0
>>> cmd = "/bin/echo" + " a" + " set" + " of" + " words"
>>> cmd
'/bin/echo a set of words'
>>> os.system(cmd)
a set of words
0

but I'm running Cygwin, so it's not really a good comparison anyway.

Steve




More information about the Python-list mailing list