Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Pass multiple arguments with values to executable via Python3?

I am trying to run a windows executable via python3 and have below code snippet. The arguments can be of the form key-value pair but its possible that few arguments may not have value like arg1 below. arg2 needs to have a value which is passed as creating arg2 variable below. The data.filepath needs to be used to construct arg2

 # data.filepath resolves to \\server1\data\inputs\filename.txt
 arg2 = "--arg2 {}".format(data.filepath)  
child = subprocess.Popen([output_path, "--arg1", arg2, "--arg3 val3", "--arg4 val4"],
                         shell=False, stderr=subprocess.STDOUT)
child.communicate()[0]
rc = child.returncode

But seems that i am not following correct syntax and getting error as below

Throw location unknown (consider using BOOST_THROW_EXCEPTION)
Dynamic exception type: class boost::exception_detail::clone_impl<struct boost::exception_detail::error_info_injector<class boost::program_options::unknown_option> >
std::exception::what: unrecognised option '--arg2 \\server1\data\inputs\filename.txt'

Please let me know the right syntax in python to pass arguments to executables properly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Apparently, the program you are running expects to receive an argument and its value as separate strings (which makes a lot of sense). You can do something like

if phase_of_moon() == 'waxing gibbous':
    arg2 = ['--arg2', data.filepath]
else:
    arg2 = []
x = Popen([output_path, '--arg1', *arg2, '--arg3', val3])

using iterable unpacking to expand arg2.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading