I started using the subprocess.Popen(Sub Process) function when I realized that the OS’s built-in functions are more powerful than I initially thought.

However, the current trend seems to focus on making core functionalities more compact while relying on external packages for additional features. subprocess.Popen is particularly useful for leveraging OS-level functions, as well as executing CMD or Shell commands directly.(Python Document)

I remember using subprocess when I first learned the concept of Popen (pipe-open) in C, especially since code often needs adjustments depending on the hardware or operating system.

In Python, subprocess.Popen is a module used to run external programs or commands. It offers powerful features that allow communication with running processes, enabling you to send and receive data seamlessly.

Key Features of subprocess.Popen

  • Execute External Commands: Run shell commands or external programs directly from Python.
  • Communicate with Processes: Utilize stdin, stdout, and stderr streams to send input to and receive output or errors from the executed processes.
  • Asynchronous Execution: Run processes asynchronously, allowing parallel execution alongside other tasks.

Sub Process Code

import subprocess, os

def subprocess_open(command):
    popen = subprocess.Popen(command, stdout=subprocess.PIPE,
            stderr=subprocess.PIPE, shell=True, encoding='euc-kr')
    (stdoutdata, stderrdata) = popen.communicate()
    return stdoutdata, stderrdata

if __name__ == "__main__":
    if os.name == 'Windows':
        stdout, stderr = subprocess_open('dir')
    elif os.name == 'posix':
        stdout, stderr = subprocess_open('ls')
    else:
        print("OS is " + os.name)
        exit()
    print("[stdout]", stdout)
    print("[stderr]", stderr)

Let’s take a look at the commands you can run from the system shell. The kinds of commands you can run depend on your operating system (Windows, Linux, macOS).

  • Common commands (Windows, Linux, macOS)
    • echo: Text output
    • ping: Check network status
  • Linux/Mac commands
    • ls: View directory listing
    • cat: View file contents
    • grep: Search for a specific string
    • curl: Request a URL
  • Windows Commands
    • dir: View a directory listing
    • tasklist: Check running processes
    • type: Print file contents

Creating small command-line (cmd) type programs and integrating them with the main program can be quite convenient. For instance, when simple operations were required in the main program, I often developed small executable programs and used subprocess.Popen to execute them without modifying the main program’s code.

Pipelines can also be used to exchange data between programs, enabling efficient data flow and communication. However, I’ve noticed that pipelines aren’t as commonly used these days. Since explaining their usage can be a bit lengthy, I’ll either cover it in detail later or leave it out for now.

By Mark

-_-

Leave a Reply

Your email address will not be published. Required fields are marked *