shell - Easily call executables from Python

FROM: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438119

Description:

One common aspect of programming is calling executables and processing results. It is not as easy or elegant to do this in Python as it is in a shell scripting language, such as bash. This script implements a shell-like module in Python which allows you to easily call executables and work with the results.

shell.py:
import sys

class Shell:
def __init__(self):
self.prefix = '/bin'
self.env = {}
self.stdout = None
self.stderr = None
self.wait = False

def __getattr__(self, command):
def __call(*args, **keywords):
if command == 'prefix':
return self.prefix
if command == 'stdout':
return self.stdout
if command == 'stderr':
return self.stderr
if command.startswith('__'):
return None
if self.prefix:
exe = '%s/%s' % (self.prefix, command)
else:
exe = command
import os, subprocess
if os.path.exists(exe):
exeargs = [exe]
if keywords:
for i in args.iteritems(): exeargs.extend(i)
if args:
exeargs.extend(args)
exeargs = [str(i) for i in exeargs]
cwd = os.path.abspath(os.curdir)
p = subprocess.Popen(exeargs, bufsize=1, cwd=cwd, \
env=self.env, stdout=subprocess.PIPE, \
close_fds=False, universal_newlines=True)
if self.wait:
ret = p.wait()
else:
ret = p.returncode
result = None
if p.stdout:
self.stdout = p.stdout.readlines()
if p.stderr:
self.stderr = p.stderr.readlines()
return ret
else:
raise Exception('No executable found at %s' % exe)
return __call

sys.modules[__name__] = Shell()

Example usage:

import shell
shell.ls('/usr', '-l')
print shell.stdout

ret = shell.ps('ax')
if not ret: print shell.stdout

你可能感兴趣的:(python)