How to use python subprocess to run c++ executable file in another folder with providing arguments, inside a python script?

Question:

I am running a python script file in which it should run a c++ executable file from another folder with some arguments.

The executable file is located in root home ubuntu i.e. (~/camera_intrinsic_calibration) folder

Generally I run on the terminal in that folder location as follows:

./pngCamCalStep1 /home/nvi/Perception/09-22-22/data/60_left/%04d.png 12 8 0.05

where ./pngcamcalstep1 is my c++ executable file and others are arguments needed to be passed.

Hence in the script file I tried the following using subprocess but none of them work:

result = subprocess.call(["./pngCamCalStep1", "home/nvi/Perception/sensor_0/left-%04d.png", "12" ,"8", "0.05"], check =True, capture_output=True, cwd='/home/nvi/camera_intrinsic_calibration/',shell =True)

or

result = subprocess.run(shlex.split("./pngCamCalStep1 home/nvi/Perception/sensor_0/left-%04d.png 12 8 0.05"), check =True, capture_output=True, cwd='/home/nvi/camera_intrinsic_calibration/', shell =True)

It doesn’t work and I get output as :

Traceback (most recent call last):
  File "/home/nvi/catkin_ws/src/camera_calibration/src/camera_calibration/camera_calibrator.py", line 340, in on_mouse
    self.c.do_calibration()
  File "/home/nvi/catkin_ws/src/camera_calibration/src/camera_calibration/calibrator.py", line 1280, in do_calibration
    result = subprocess.call(["./pngCamCalStep1", "home/nvi/Perception/sensor_0/left-%04d.png", "12" ,"8", "0.05"], check =True, capture_output=True, cwd='/home/nvi/camera_intrinsic_calibration/',shell =True)
  File "/usr/lib/python3.8/subprocess.py", line 340, in call
    with Popen(*popenargs, **kwargs) as p:
TypeError: __init__() got an unexpected keyword argument 'check'

Can anyone please let me know how to solve this problem?

What is the right command to call or run a C++ executable file from another folder with providing it’s input arguments?

Asked By: Nagarjun Vinukonda

||

Answers:

Your syntax is mostly correct.

The error message is quite clear: subprocess.call(), which use subprocess.Popen class as backend, does not accept a keyword argument ‘check’

Remove that argument and try again.

If you want CalledProcessError to be raised when the called process return non-zero returncodes, use subprocess.check_call() instead.

Answered By: Ligon Liu

Well, I have kept the whole argument in a single quote, then it worked, removing, check and capture_output:

subprocess.call(["./pngCamCalStep1 home/nvi/Perception/sensor_0/left-%04d.png 12 8 0.05"], cwd='/home/nvi/camera_intrinsic_calibration/',shell =True)
Answered By: Nagarjun Vinukonda