Skip to content

Instantly share code, notes, and snippets.

@GaryLee
Last active October 2, 2017 13:00
Show Gist options
  • Save GaryLee/d4035d13a26e5a3b9f453e7a3e9fba51 to your computer and use it in GitHub Desktop.
Save GaryLee/d4035d13a26e5a3b9f453e7a3e9fba51 to your computer and use it in GitHub Desktop.
Execute shell command in Perl-like manner.
import sys
import os
from subprocess import CalledProcessError, check_output
def cmd(cmd_arg):
"""Execute shell command in Perl-like manner."""
ctx = globals().copy()
ctx.update(sys._getframe(1).f_locals)
for ln in check_output(cmd_arg.format(**ctx), shell=True).split(os.linesep):
yield ln
filelist = '*.txt'
pattern = 'xyz'
for ln in cmd('dir {pattern} {filelist}'):
print ln
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment