您的位置:首页 > 编程语言 > Python开发

Python 执行linux 命令

2018-05-02 14:25 309 查看
# !/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.

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Monaco; color: #f4f4f4; background-color: rgba(0, 0, 0, 0.85) }
span.s1 { }
span.s2 { color: #cd7923 }
tmp = os.popen('ps aux|grep uwsgi').readlines()

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: