Hey guys:
I'm trying to write a python script to execute a Linux program, with several arguments set by the user, like so:

import os
variable_input=int(input("Input Number Argument: "))
os.system("/path/to/program"," -argument1 ",variable_input)

However, the os.system() command doesn't allow more than one input, so I tried this:

import os
variable_input=int(input("Input Number Argument: "))
final_command="/path/to/program"," -argrument1 ",variable_input
os.system(final_command)

Only to find out that the os.system() command will only accept string input (that is, quotes around the input). That won't work, for obvious reasons. Is there some workaround, or an alternative to the os.system() command?

Thanks in advance!

Recommended Answers

All 4 Replies

Tried the subprocess module? Try the call() function.

Tried the subprocess module? Try the call() function.

After trying that like so,

import subprocess
variable_input=int(input("Input Number Argument: "))
final_command="/path/to/program"," -argrument1 ",variable_input
subprocess.call(final_command)

I was given this error:

File "./source_run.py", line 141, in <module>
    subprocess.call(final_command)
  File "/usr/lib/python2.5/subprocess.py", line 444, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.5/subprocess.py", line 594, in __init__
    errread, errwrite)
  File "/usr/lib/python2.5/subprocess.py", line 1153, in _execute_child
    raise child_exception
OSError: [Errno 13] Permission denied

What am I doing wrong?

P.S.: I'm running it as root, so I don't see permission problems as being the cause of this.

The thing that you are doing wrong is that you are not putting everything together into a single string.

It is easiest to use string formatting in order to create the system command that you are looking for. For instance you can do the following:

program_str = 'python'
args = '-V -3'
file_to_run = 'tempPy.py'
cmd_str = ''.join([program_str, ' ', args, ' ', file_to_run])
os.system(cmd_str)

If you have your arguments stored as a list it might be easiest to write a helper function to create the argument string

args_list = ['-V', '-3']
def mk_args_str(args_list):
    print ('%s '*len(args_list) % tuple(args_list).rstrip()

formatted_args = mk_args_str(args_list)

The thing that you are doing wrong is that you are not putting everything together into a single string.

It is easiest to use string formatting in order to create the system command that you are looking for. For instance you can do the following:

program_str = 'python'
args = '-V -3'
file_to_run = 'tempPy.py'
cmd_str = ''.join([program_str, ' ', args, ' ', file_to_run])
os.system(cmd_str)

If you have your arguments stored as a list it might be easiest to write a helper function to create the argument string

args_list = ['-V', '-3']
def mk_args_str(args_list):
    print ('%s '*len(args_list) % tuple(args_list).rstrip()

formatted_args = mk_args_str(args_list)

Thanks, that did it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.