Basic cygwin commands from python subprocess

When working with Python, it is often necessary to execute commands in the command line or terminal. One way to achieve this is by using the subprocess module, which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

Option 1: Using

The function was introduced in Python 3.5 and provides a high-level interface for executing shell commands. It takes a list of command-line arguments as input and returns a CompletedProcess object that contains information about the executed command.

import subprocess

command = ['ls', '-l']
result =, capture_output=True, text=True)


In this example, we are using the ‘ls’ command with the ‘-l’ option to list the files and directories in the current directory. The capture_output=True argument captures the command’s output, and the text=True argument ensures that the output is returned as a string.

Option 2: Using subprocess.Popen()

If you need more control over the execution of the command or want to interact with the command’s input/output/error streams, you can use the subprocess.Popen() function. This function allows you to spawn a new process, connect to its pipes, and communicate with it.

import subprocess

command = ['ls', '-l']
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

stdout, stderr = process.communicate()


In this example, we are using the ‘ls’ command with the ‘-l’ option again. The stdout=subprocess.PIPE argument redirects the command’s standard output to a pipe, and the stderr=subprocess.PIPE argument redirects the standard error to another pipe. The process.communicate() method reads the command’s output and error streams, returning them as strings.

Option 3: Using os.system()

The os.system() function is a simple way to execute shell commands, but it has some limitations compared to the subprocess module. It returns the exit status of the command and does not capture the command’s output or error streams.

import os

command = 'ls -l'

In this example, we are using the ‘ls -l’ command as a string. The os.system() function executes the command and returns its exit status.

After considering the three options, the best choice for executing basic Cygwin commands from Python is Option 1: Using This option provides a high-level interface, captures the command’s output, and allows for easy manipulation of the returned data.

Rate this post

11 Responses

    1. Its a no-brainer, my friend. Option 3 all the way! Sometimes familiarity beats simplicity. Plus, you cant put a price on the comfort of an old friend. Trust me, you wont regret it.

  1. Option 1 seems pretty straightforward, but Option 2 gives more control. What about Option 3 though? Anyone tried it?

    1. Option 3 is a disaster waiting to happen. Its unstable and prone to glitches. Stick with Option 1 if you want simplicity or go for Option 2 if you crave control. Dont waste your time with Option 3, trust me.

  2. Option 1 is the way to go! makes life so much easier. Who needs the hassle of Popen() or os.system()?

    1. Wow, I actually still use os.system() sometimes. It may not be the trendiest option, but it gets the job done for simple tasks. Different tools for different needs, right?

Leave a Reply

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

Table of Contents