Using os.system() and string concatenation

Discussion in 'Python' started by Wayne Witzel III, Oct 12, 2004.

  1. Using Python 2.3

    Currently I process three files and build the output of those files in
    to lists using for statements.

    I think take those lists and provide them to an os.system() call.

    cmd = "/usr/sbin/useradd"
    os.system(cmd + list1[0] + list1[1] + list2[0] + list3[0])

    This executes without any errors, but doesn't execute the command
    supplied to os.system(). Now if I place them all in cmd first, then
    supply it to os.system() it executes just fine. So there really isn't
    a problem, just wanting to know what would cause such behavior.

    Thanks,

    Wayne
    Wayne Witzel III, Oct 12, 2004
    #1
    1. Advertising

  2. Wayne Witzel III

    Donn Cave Guest

    In article <>,
    Wayne Witzel III <> wrote:

    > Using Python 2.3
    >
    > Currently I process three files and build the output of those files in
    > to lists using for statements.
    >
    > I think take those lists and provide them to an os.system() call.
    >
    > cmd = "/usr/sbin/useradd"
    > os.system(cmd + list1[0] + list1[1] + list2[0] + list3[0])
    >
    > This executes without any errors, but doesn't execute the command
    > supplied to os.system(). Now if I place them all in cmd first, then
    > supply it to os.system() it executes just fine. So there really isn't
    > a problem, just wanting to know what would cause such behavior.


    As already mentioned in other followups, could be white
    space missing.

    On the other hand, this looks to me like a very good place to
    use os.spawnv instead of os.system. It might resolve the
    present problem, but much more importantly, it will avoid
    more dangerous problems of a similar nature. When you construct
    a shell command out of data from files, data becomes shell
    syntax, and there is in theory the possibility that the result
    will be worse than just invalid, it may execute a different
    command or different parameters than you had in mind, with a
    wide range of potentially unpleasant results.

    But spawnv() executes the command directly, with the parameters
    you supply, unlike system() which invokes the shell. So with
    spawnv() you don't need white space, but more importantly the
    only command that can run as a result is the one you specify.

    os.spawnv(os.P_WAIT, '/usr/sbin/useradd',
    ['useradd', list1[0], list1[1], list2[0], list3[0]])

    Donn Cave,
    Donn Cave, Oct 12, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    4,774
    walala
    Sep 18, 2003
  2. Wayne Witzel III

    Re: Using os.system() and string concatenation

    Wayne Witzel III, Oct 12, 2004, in forum: Python
    Replies:
    2
    Views:
    370
    Wayne Witzel III
    Oct 17, 2004
  3. Replies:
    12
    Views:
    1,283
  4. Andrew Berg
    Replies:
    13
    Views:
    1,254
    Andrew Berg
    Jul 11, 2011
  5. pachl
    Replies:
    6
    Views:
    134
    pachl
    Feb 20, 2008
Loading...

Share This Page