Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Python Popen that does not suck.

A Python Popen that does not suck.

Function

import os
import subprocess

def popen(*args, **kwargs):
    output      = kwargs.get('output', False)
    directory   = kwargs.get('dir')
    environment = kwargs.get('env')
    shell       = kwargs.get('shell', True)
    raise_err   = kwargs.get('raise_err', True)

    environ     = os.environ.copy()
    if environment:
        environ.update(environment)

    command     = " ".join([str(arg) for arg in args])
    
    proc        = subprocess.Popen(command,
        stdin   = subprocess.PIPE if output else None,
        stdout  = subprocess.PIPE if output else None,
        stderr  = subprocess.PIPE if output else None,
        env     = environ,
        cwd     = directory,
        shell   = shell
    )

    code       = proc.wait()

    if code and raise_err:
        raise subprocess.CalledProcessError(code, command)

    if output:
        output, error = proc.communicate()

        if output:
            output = output.decode('utf-8')
            if output.count('\n') == 1:
                output = output.strip('\n')

        if error:
            error  =  error.decode('utf-8')
            if  error.count('\n') == 1:
                error  =  error.strip('\n')

        return code, output, error
    else:
        return code

Usage

>>> popen('touch foobar.txt')
@mals14

This comment has been minimized.

Copy link

mals14 commented Jan 30, 2019

Thanks for sharing. If you annotated, would be even more powerful... :)

@nils-werner

This comment has been minimized.

Copy link

nils-werner commented Jan 30, 2019

Why are you using variadic parameters when you don't even rely on them?

@achillesrasquinha

This comment has been minimized.

Copy link
Owner Author

achillesrasquinha commented Feb 6, 2019

>>> popen('echo $FOOBAR', env = { 'FOOBAR': 'barbaz' })
barbaz
@reorx

This comment has been minimized.

Copy link

reorx commented Nov 26, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.