Skip to main content

popen

What is it?

The popen package provides a simple shell-like syntax inside python for running external commands.

Features:

  • Chaining (aka. piping) as Sh('ls') | 'sort'

  • Redirect stderr to stdout as Sh('make').include_stderr | 'wc'

  • Redirect output to file as Sh('ls') > '~/listing.txt'

  • Iteration over output lines as for line in Sh('ls'):

  • Streaming input into commands in the chain as Sh.pipe('~/listing.txt').Sh('grep', '-q', 'code').returncode

  • Expands special characters (~*!?)

  • Expands env vars ($HOME) as print Sh('ls', '$USER', '~/*')

  • Properly splits strings ('echo "hole in one"' becomes ['echo', 'hole in one'])

  • Or iterable arguments (Sh('ls', '-l') | ['wc', '-l'] > '/dev/null')

TL;DR

Installation:

pip install popen

Example:

from popen import Sh
for line in Sh('du', '~/') | 'head -n 10' | 'sort -n':
    print('GOT', line)

Usage

Simples usage, run a command and iterate the output lines:

from popen import Sh
for line in Sh('ls', '-la', '~'):
    print line

Piping that output to a file:

Sh('ls', '-la', '~') > '~/listing'

Note that special characters like ~ are expanded, as are environment variables (e.g. $HOME).

Chaining commands is trivial, here with append instead of write to file:

Sh('ls') | Sh('sort') >> '~/listing'

But the right hand side of | can be very flexible, as it employs lexical splitter on string input or takes an iterable:

Sh('ls', '-la', '~') | 'sort -c' | ['uniq', '-c'] | 'tail' | Sh('wc') > '~/listing'

To run a command and let output go to stdout, ask for the return code:

cmd = Sh('ls') | 'grep polka'
print cmd.returncode

To pipe a string into a command, wrap in an iterable:

print Sh.pipe(['this is a \nmulti line\nstring.']) | 'wc'

Metadata

Release files for popen 0.1.20

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for popen 0.1.20
File Size Uploaded Provenance Actions
popen-0.1.20.tar.gz 7.0 kB Without provenance Details for popen-0.1.20.tar.gz

Release files / popen-0.1.20.tar.gz

Download URL popen-0.1.20.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9271dc3525fd34c1105acc106044ae01580830fd79361ad83c34d7cdfb6b385f
BLAKE2b-256 checksum
How to use checksums
04b12bb086fa75a407ff446963ebabc7a91538c4b6c64499f857d94709be54fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for popen 0.1.20

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page