Optionally pass argument to subprocess in python -
in script call executable optional command line argument (if passed, argument switches on function in executable, off default). want control option directly argument of script itself. write do:
import subprocess option = false if option: check = subprocess.popen(['executable', '--option']) else: check = subprocess.popen(['executable'])
i tried find more compact way:
import subprocess option = false check = subprocess.popen(['executable', '--option' if option else ''])
but raises error "unknown option" in exactable itself.
and raises error in python:
import subprocess option = false check = subprocess.popen(['executable', '--option' if option else none])
so, question: there way pass optional argument subprocess in 1 line?
using list slicing:
>>> ['executable', '--option'][:1 + false] ['executable'] >>> ['executable', '--option'][:1 + true] ['executable', '--option'] >>> false == 0 true >>> true == 1 true
check = subprocess.popen(['executable', '--option'][:1 + option])
update alternative
you can use list
* bool
:
>>> ['a'] * true ['a'] >>> ['a'] * false []
using this, can combine multiple options:
check = subprocess.popen(['executable'] + ['-option1'] * option1 + ['-option2'] * option2)
Comments
Post a Comment