Search code examples
pythonbashpipsubprocesspopen

subprocess.call() in python is used for?


roadSegCommand = segBin + pred_img + self.out_dir # Here we are adding 3 links
output = subprocess.call(['bash','-c', roadSegCommand])

Bash is not working in my pc so i used "pip" instead of "bash". If i place -c there, no such options -c is comming on console. So if i changed to -h it was working. But my questions are

  1. Is it correct subprocess.call(["pip","-h",roadSegCommand]) ?

  2. What is the difference between subprocess call and popen.

  3. Is it a correct notation in parenthesis roadSegCommand?

  4. Will i use pip instead of bash. What's the difference between them?


Solution

  • subprocess is used to run OS commands

    pip -h shows the help contents of the pip command, and takes no arguments, so 1) no, not correct

    bash -c "command statement" will run that statement using bash

    Python nor pip will help you to run bash commands if bash is not available to your system, so 4) they're completely separate programs entirely

    All your other questions are answered by the subprocess module docs - https://docs.python.org/3/library/subprocess.html specifically, you can either call a process or open it to get its output and running status, although using run() is recommended over either

    The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle

    3) yes, you have valid Python syntax