Python 执行linux 命令

# !/usr/bin/env python
# -*- coding: utf-8 -*

import os

def main():

    file_list = os.popen("ls").read().split()
    for f in file_list:
        if '.log' in f:
            cmd = "tar zcvf {}.tar.gz {}".format(f[:-4], f)
            os.system(cmd)
            print 'success'


if __name__ == '__main__':
    main()

 

 

2.os.popen

# 该方法不但执行命令还返回执行后的信息对象

popen(command [, mode='r' [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.

tmp = os.popen('ps aux|grep uwsgi').readlines()

 

 

posted on 2018-05-02 14:25  星河赵  阅读(515)  评论(0编辑  收藏  举报

导航