@bergus
2015-12-11T10:33:50.000000Z
字数 1374
阅读 2056
pyshell
python
shell
pyshell 调用外部命令,让你像执行bash一样方便
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from subprocess import Popen
from subprocess import PIPE
def py_ver():
'''
得到python的版本
'''
import sys
return sys.version_info[0]
_ver = py_ver()
if _ver == 2:
builtin_str = str
bytes = str
str = unicode
basestring = basestring
numeric_types = (int, long, float)
elif _ver == 3:
builtin_str = str
str = str
bytes = bytes
basestring = (str, bytes)
numeric_types = (int, float)
else:
raise ValueError(u'python 版本不正确')
del _ver
# 解析字符串中的环境变量
def parse_shell_token(t):
import os
# 将~等用用户的家目录进行替换
t = os.path.expanduser(t)
# path中可以使用环境变量,'$PATH'...
t = os.path.expandvars(t)
return t
class cmd(object):
def __init__(self, *args, **kwargs):
self.stdout = None
self.cmd(*args, **kwargs)
def cmd(self, cmd, env=None, stdout=PIPE):
p = Popen(parse_shell_token(cmd), shell=True,
stdout=stdout, stdin=PIPE, stderr=PIPE, env=env)
self.stdout, self.stderr = p.communicate(input=self.stdout)
self.code = p.returncode
return self
def __repr__(self):
return self.value()
def __unicode__(self):
return self.value()
def __str__(self):
return self.value()
def __nonzero__(self):
return self.__bool__()
def __bool__(self):
return bool(self.value())
def value(self):
if not self.stdout:
return ''
return self.stdout.strip()
if __name__ == '__main__':
print cmd('ls -al')
print cmd("ls | grep 'LICENSE'")
print cmd("konsole --hold -e 'konsole --help'")
print cmd('ls $HOME')
print cmd('ls ~')
dd = cmd('ls ~').value().split('\n')
print dd
cmd("gnome-terminal -x bash -c 'python -h;read' ")