0

I want to create a method which executes below command in python using subprocess.run()

python3 detect_wrong.py --source overpass.mp4 --weights ./my_coco.pt --data ./data/my_coco.yaml

subprocess.run(["python","detect_wrong.py"])

I am stuck in this step. I want to know how to pass those arguments.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
AaqilSh
  • 129
  • 2
  • 5

4 Answers4

0

As explained here: https://docs.python.org/3/library/subprocess.html, you can add in the array every argument you would normally pass in a command-line.

For example, if you wanted to run ls -l --color=auto, you could write:

subprocess.run(["ls", "-l", "--color=auto"])

Here, that would be:

subprocess.run(["python3", "detect_wrong.py", "--source", "overpass.mp4", "--weights", "./my_coco.pt", "--data", "./data/my_coco.yaml"])

However, since you want to run a Python script from Python, I suggest you take a look here: How can I make one python file run another? and, as said there, treat detect_wrong.py as a module, if possible. Else, you can run it with exec() or, if desperate, with os.system() or subprocess.run()

0

you can simply pass arguments as entities on the list, like that:

subprocess.run(["python", "--argument=value", "detect_wrong.py"]
Matmozaur
  • 283
  • 2
  • 6
0

To execute the command python3 detect_wrong.py --source overpass.mp4 --weights ./my_coco.pt --data ./data/my_coco.yaml using the subprocess module in Python, you can modify the previous example code as follows:

import subprocess

command = ['python3', 'detect_wrong.py', '--source', 'overpass.mp4', '--weights', './my_coco.pt', '--data', './data/my_coco.yaml']
subprocess.run(command)

In this code, the command is specified as a list where each element represents a part of the command and its arguments. The first element is 'python3' to specify the Python interpreter, followed by 'detect_wrong.py' to indicate the script name, and then the command-line arguments --source, overpass.mp4, --weights, ./my_coco.pt, --data, and ./data/my_coco.yaml.

Make sure to adjust the command and arguments based on your specific use case, including the correct path to the files used.

  • This answer looks like it was generated by an AI (like ChatGPT), not by an actual human being. You should be aware that [posting AI-generated output is officially **BANNED** on Stack Overflow](https://meta.stackoverflow.com/q/421831). If this answer was indeed generated by an AI, then I strongly suggest you delete it before you get yourself into even bigger trouble: **WE TAKE PLAGIARISM SERIOUSLY HERE.** Please read: [Why posting GPT and ChatGPT generated answers is not currently allowed](https://stackoverflow.com/help/gpt-policy). – tchrist Jul 11 '23 at 13:33
0

simply append arguments in the list of subprocess.run

subprocess.run(["python","detect_wrong.py","--source=overpass.mp4", "--weights=./my_coco.pt", "--data=./data/my_coco.yaml"])
dolker
  • 3
  • 2