How to run application with parameters in Python?

前端 未结 3 1572
礼貌的吻别
礼貌的吻别 2020-12-30 06:31

I need to run an application (binary file) and pass arguments using a Python code. Some arguments represent strings got during Python file processing.

for i          


        
相关标签:
3条回答
  • 2020-12-30 06:56

    All you need to do is include it in the argument list, instead of as a separate argument:

    subprocess.call(["test.exe", files[i]])
    
    0 讨论(0)
  • 2020-12-30 07:01

    With regard to your updated question: The arguments for your subprocess are not passed as individual parameters to call(); rather, they're passed as a single list of strings, like so:

    args = ["test.exe", "first_argument", "second_argument"]
    

    Original response: The code as you have it will create a separate process for each element in files. If that's what you want, your code should work. If you want to call the program with all of the files simultaneously, you'll want to concatenate your list:

    args = ["test.exe"] + files
    subprocess.call(args)
    
    0 讨论(0)
  • 2020-12-30 07:03
    args = ['test. exe']
    subprocess.call(args, '-f')  //Error
    

    should be:

    args = ['test.exe', '-f']
    subprocess.call(args) 
    

    The command line argument should all be inside a single list for the first parameter of subprocess.call. The second argument to call is bufsize, which is supposed to be an integer (hence why you get the error you do)

    0 讨论(0)
提交回复
热议问题