用python执行Linux命令
例1:在python中包装ls命令
#!/usr/bin/env python
#python wapper for the ls command
import subprocess
subprocess.call(["ls","-l"])
[root@pydb python]# python ls.py
total 8
-rwxrwxrwx 1 root root 415 Mar 18 11:40 a.py
-rw-r--r-- 1 root root 103 May 4 22:17 ls.py
[root@pydb python]# python ls.py
#!/usr/bin/env python
#python wapper for the ls command
import subprocess
subprocess.call(["cat","/python/ls.py"])
以上是执行Linux下的命令和参数。非常不错的一个例子,好好记住吧
#!/usr/bin/env python
#A System Information Gathering Script
import subprocess
#command 1
uname = "uname"
uname_arg = "-a"
print "Gathering system information with %s command:\n" % uname
subprocess.call([uname,uname_arg])
#command 2
diskspace = "df"
diskspace_arg = "-h"
print "Gathering diskspace information %s command:\n" % diskspace
subprocess.call([diskspace,diskspace_arg])
[root@pydb python]# python info.py
Gathering system information with uname command:
Linux pydb 2.6.18-308.el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU /Linux
Gathering diskspace information df command:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
37G 3.9G 31G 11% /
/dev/sda1 99M 13M 82M 13% /boot
tmpfs 506M 0 506M 0% /dev/shm
[root@pydb python]#
用python执行Linux命令的更多相关文章
- python执行linux命令的两种方法
python执行linux命令有两种方法: 在此以Linux常用的ls命令为例: 方法一:使用os模块 1 2 3 shell# python >> import os >> ...
- 再见Xshell、Xftp!Python执行Linux命令、上传下载远程文件
相信大家应该都接触过Linux操作系统(Ubuntu.Centos等),那么在使用的Linux操作系统需要使用一些远程ssh工具,尤其是公网服务器. 常用的ssh工具主要有:Xshell.MobaXt ...
- 使用python执行linux命令
python版本是2.7.12 一.简单的获取linux命令的执行结果,比如:获取一个PID的进程树结构,linux命令是pstree -p pid,在python中有一个模块可以方便的获取.至于有时 ...
- Python 执行linux 命令
# !/usr/bin/env python # -*- coding: utf-8 -* import os def main(): file_list = os.popen("ls&qu ...
- [Python]执行Linux命令
使用subprocess模块 import subprocess # 防火墙服务存在关闭状态 child1 = subprocess.Popen(["systemctl status fir ...
- 解决python执行linux查看文件命令后,因为^@特殊符号导致部分内容不显示的问题
问题现象 文件a的内容如下: 查看第3行到第5行的内容:sed -n '3,5p' /tmp/test/a 可见在命令行执行,是没有问题的. 在python中使用paramiko模块执行linux命令 ...
- Python实现Linux命令xxd -i功能
目录 Python实现Linux命令xxd -i功能 声明 一. Linux xxd -i功能 二. xxd -i常见用途 三. 类xxd -i功能的Python实现 Python实现Linux命令x ...
- Python执行Linux系统命令方法
Python执行Linux系统命令的4种方法 (1) os.system 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 复制代码代码如下: system(command) -> ...
- 使用Android平板编程,执行linux命令
android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/app ...
随机推荐
- linux自建https证书
一.生成单向认证的https证书 建立服务器私钥,生成RSA秘钥. 会有两次要求输入密码, 然后获得了一个server.key文件. 以后使用此文件(通过openssl提供的命令或API)可能经常回要 ...
- Python三级菜单作业实现
数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, ...
- Difference between JDK, JRE and JVM
With Java programming language, the three terms i.e. JDK, JRE and JVM will always be there to unders ...
- abp去掉AbpUser中的Name,Surname,去掉姓和名分离
abp是国外的框架,默认的框架中的AbpUser表中的Name和Surname是分开的,这不符合国情:可以先去掉 1. 在User类中重写Name和Surname,并设置为私有 2. 在DbConte ...
- 清新水彩追梦在路上述职报告通用PPT模板
清新漫画风,轻松可爱模版来源: http://ppt.dede58.com/gongzuohuibao/26566.html
- CAD简易口诀,保你一天就记住!零基础也能轻松学!CAD制图宝典!
如何才能快速的学习CAD制图呢?不仅仅需要多练习,CAD口诀也是不能错过的哦!实用干货这一个就够了快点收藏起来! 1.创建直线的快捷方式是L+空格 2.创建圆的快捷方式是C+空格 3.创建圆弧的快捷方 ...
- boostrap 学习笔记
bootstrap : 是全球最受欢迎的前端组件库,用于开发响应式布局.移动设备优先的 WEB 项目. 用于项目样式的快速搭建,真的是..特别快.. 随便找两个cdn引用就能使用了. https:// ...
- 个人项目开源之Django图书借阅系统源代码
Django写的模拟图书借阅系统源代码已开源到码云 源代码
- 036.[转] JNDI 学习
使用外置服务器(如tomcat)时,如果一个服务器启动多个项目,可以使用JNDI配置数据源,这样每个项目都可以获取到Tomcat 配置的 JNDI的数据源. 在学习 jsp 的时候,作用域对象 pag ...
- 【分享】nginx负载均衡全套视频教程
1.课件 百度网盘链接:https://pan.baidu.com/s/1On2oONVZmPwI9yIDekgRiA 提取码:c4fw 2.教程列表 3.教程下载 3.1.直接在线学习 ...