I would like to check if a script is running with a specific command line argument within a python script.
For example I would like to check if:
main.py testarg
Is running. Is there any way I can achieve this?
Thanks in advance
I would like to check if a script is running with a specific command line argument within a python script.
For example I would like to check if:
main.py testarg
Is running. Is there any way I can achieve this?
Thanks in advance
To search through the currently running processes, you should use a library such as psutil
to ensure maximum platform compatibility.
import psutilfor process in psutil.process_iter():cmdline = process.cmdline()if "main.py" in cmdline and "testarg" in cmdline:# do something
If instead you are looking to search through the arguments of the current process you can use the sys.argv
list:
import sysif "testarg" in sys.argv:# do something
For more complex argument parsing, it is recommended to use argparse
.