'Run shell in subprocess.run(cmd_line) using one line cmd string

os.system() invokes a command, the argument can be a single string, e.g,

os.system("cmd -input xx --output yy").

But subprocess, I have to pass a list for args, e.g.,

subprocess.run(["cmd", "-input", "xx", "--output", "yy"]).

For complex arguments, passing a list is trivial. So how to pass a single string to run a command and can also try exceptions?

Thanks.



Solution 1:[1]

cmd_string = 'cmd -input xx --output yy'

subprocess.run(cmd_string.split())

Solution 2:[2]

you can simply split the command string and pass it to subprocess like bellow:
subprocess.run("your cmd command".split())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 balu
Solution 2 No.BoD