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()
相关文章推荐
- Python执行Linux系统命令的4种方法
- IPython下执行shell命令-魔力函数(alias store rehash)- 千月的python linux 系统管理指南学习笔记(6)
- Python subprocess,Linux下执行shell命令
- 在C/python中执行linux命令并得到返回值以及输出
- 在C/python中执行linux命令并得到返回值以及输出
- python写的linux系统批量执行命令和文件获取和推送功能(基于RSAkey)
- Python执行Linux系统命令的4种方法
- python中执行linux命令(调用linux命令)
- 利用python多线程执行远程linux上命令
- 【linux】终端直接执行py文件,不需要python命令
- python paramiko 向linux执行命令和发送接收文件
- Python之连接linux并执行命令
- 先执行linux的clear清屏命令,再执行其他操作 分类: python 小练习 2013-09-13 11:23 441人阅读 评论(0) 收藏
- Python远程登录Linux操作系统,执行命令、创建目录、上传及下载文件
- Python远程登录Linux操作系统,执行命令、创建目录、上传及下载文件
- Python批量操作Linux服务器执行命令
- python中执行linux命令(调用linux命令)
- python学习五:执行linux命令的两种方法、提高运行效率、进制转换、切片详解、ip和mac获取
- Python 调用os执行类似linux/dos常见命令
- Python执行Linux系统命令的4种方法