Issue
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.
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
.
Answered By - Ture Pålsson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.