Qoders Blog     About     Archive     Feed
by Qadeer Ahmad Khan

Running shell commands with Python

Although bash scripts are powerful, the language contains many gotchas that makes your head scratch. I therefore really enjoy the simplicity of Python. The following is a simple way to run shell command and get the result, using Python. Note that output to stderr will not be returned.

import subprocess

    def run_cmd(command):
        return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read()

Usage example - find maven dependencies, do some filtering and print the result:

dependencies='mvn dependency:list -DexcludeTransitive=true | grep -v Download | grep -v test | sed -n "s/^[^:]*:\([^:]*:.*\)/\\1/p" | sort'
print run_cmd(dependencies).split()
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy