分享

python 中运行linux shell脚本

 java_laq小馆 2013-12-26
 

python 中运行linux shell脚本

分类: python 585人阅读 评论(0) 收藏 举报
subprocess.Popen(command, shell=True)

如果command不是一个可执行文件,shell=True不可省。

最简单的方法是使用class subprocess.Popen(command,shell=True)。

Popen类有Popen.stdin,Popen.stdout,Popen.stderr三个有用的属性,可以实现与子进程的通信。

例如:

  1. handle = subprocess.Popen('ls -l', stdout=subprocess.PIPE, shell=True)  
  2. print handle.stdout.read()  
  3. print handle.communicate()[0]  
python 去掉特殊字符

lstrip和rstrip 分别去首尾字符

例如:

  1. theString = 'saaaay yes no yaaaass'  
  2. print theString.strip('say')  
  3. print theString.strip('say ') #say后面有空格  
  4. print theString.lstrip('say')  
  5. print theString.rstrip('say')   

结果:

  1. yes no  
  2. es no  
  3. yes no yaaaass  
  4. saaaay yes no   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多