Using os.system() and string concatenation
Michael Hoffman
m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Tue Oct 12 18:11:13 EDT 2004
Wayne Witzel III wrote:
> 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.
This is very odd, Wayne, and somewhat unexpected. What happens if you
replace /usr/sbin/useradd with /bin/echo (or wherever your echo command is):
>>> import os
>>> user = ["foo", "bar", "baz", "moo"]
>>> os.system("/bin/echo"+ " -d " + user[0] + " -s " + user[1] + " " +
user[3])
-d foo -s bar moo
0
>>> cmd = "/bin/echo"+ " -d " + user[0] + " -s " + user[1] + " " + user[3]
>>> os.system(cmd)
-d foo -s bar moo
As others have said, os.spawnv() is probably a better choice anyway...
--
Michael Hoffman
More information about the Python-list
mailing list