每天写点python
1、收集系统信息python小程序
1 #!/usr/bin/env python
2 #A system information gathering script
3
4 import subprocess
5 #command 1
6 uname = "uname"
7 uname_arg = "-a"
8 print "Gathering system information with %s command:\n" % uname
9 subprocess.call([uname, uname_arg])
10
11 #command 2
12 diskspace = "df"
13 diskspace_arg = "-h"
14 print "Gathering diskspace information %s command:\n" % diskspace
15 subprocess.call([diskspace, diskspace_arg])
以上实例执行结果为:
# python system_information.py
Gathering system information with uname command: Linux gio016 2.6.32-431.29.2.lustre.el6.x86_64 #1 SMP Fri Jul 31 09:39:58 CST 2015 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command: Filesystem Size Used Avail Use% Mounted on
/dev/sda3 258G 71G 174G 29% /
tmpfs 32G 0 32G 0% /dev/shm
/dev/sda1 485M 142M 318M 31% /boot
/dev/memdiska 1.1T 132G 914G 13% /home/export/tmp
2、收集系统信息(定义为多个函数)
1 #!/usr/bin/env python
2 import subprocess
3
4 def uname_func():
5 uname = "uname"
6 uname_arg = "-a"
7 print "Gathering system information with %s command:\n" % uname
8 subprocess.call([uname, uname_arg])
9
10 def disk_func():
11 diskspace = "df"
12 diskspace_arg = "-h"
13 print "Gathering diskspace information %s command:\n" % diskspace
14 subprocess.call([diskspace, diskspace_arg])
15
16 def main():
17 uname_func()
18 disk_func()
19
20 main()
以上实例输出结果为:
# python system_information1.py
Gathering system information with uname command: Linux gio016 2.6.32-431.29.2.lustre.el6.x86_64 #1 SMP Fri Jul 31 09:39:58 CST 2015 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command: Filesystem Size Used Avail Use% Mounted on
/dev/sda3 258G 71G 174G 29% /
tmpfs 32G 0 32G 0% /dev/shm
/dev/sda1 485M 142M 318M 31% /boot
/dev/memdiska 1.1T 132G 914G 13% /home/export/tmp
3、使用shell实现该功能
#!/usr/bin/env bash function uname_func()
{
uname="uname -a"
printf "Gathering system information with the $uname command:\n\n"
$uname
} function disk_func()
{
diskspace="df -h"
printf "Gathering diskspace information with the $diskspace command:\n\n"
$diskspace
} function main()
{
uname_func
disk_func
} main
4、fork进程
1 #!/usr/bin/env python
2 import os
3 pid = os.fork()
4 # fork and exec together
5 print("second test")
6 if pid == 0: #This is the child
7 print("this is the child")
8 print("I'm going to exec another program now")
9 os.execl('/bin/ls', 'ls','/etc/passwd')
10 else:
11 print("the child is pid %d" % pid)
12 os.wait()
~
以上实例执行结果为:
# python fork2.py
second test
the child is pid 5341
second test
this is the child
I'm going to exec another program now
/etc/passwd
5、db数据库
1 #!/usr/bin/env python
2 import dbm
3 db = dbm.open('websites', 'w')
4 db['www.wrox.com'] = 'Wrox home page'
5 if db['www.python.org'] != None:
6 print('Found www.python.org')
7 else:
8 print('Error: Missing item')
9
10 for key in db.keys():
11 print("key =",key," value =", db[key])
12
13 del db['www.wrox.com']
14 print("After deleting www.wrox.com, we have:")
15
16 for key in db.keys():
17 print("key =",key," value =",db[key])
18
19 db.close()
以上实例结果:
# python dbmaccess.py
Found www.python.org
('key =', 'www.wrox.com', ' value =', 'Wrox home page')
('key =', 'www.python.org', ' value =', 'Python home page')
After deleting www.wrox.com, we have:
('key =', 'www.python.org', ' value =', 'Python home page')
6、产生随机密码
1 #!/usr/bin/env python
2 from random import choice
3 import string
4 def GenPasswd(length=8, chars=string.letters+string.digits):
5 return ''.join([ choice(chars) for i in range(length)])
6
以上实例执行的结果:
>>> import makepass
>>> for i in range(6):
... print makepass.GenPasswd(12)
...
4sxiLRE6bqAX
QSP2ZXjSsMxl
DLygnUv5T5qq
skL7pb5Zhhvp
GdJnnJKG5mZN
6gmow2qv47Tg
7、查看当前目录是否有(.py) python程序
>>> import os
>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
每天写点python的更多相关文章
- 将自己写的Python代码打包放到PyPI上
如果是开源的Python代码,为了能够让大家更方便的使用,放到PyPI上也许是个非常不错的主意(PyPI:Python Package Index).刚开始我以为要将代码打包放到PyPI上是一件非常复 ...
- 写一个python的服务监控程序
写一个python的服务监控程序 前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4. 到python网站下载源代码,解压到Redhat上, ...
- 我写的python代码的规则
1.Python文件的命名: 采用每个单词的首字母大写,不使用下划线 2.Python类的命名: 采用每个单词的首字母大写,不使用下划线 3.Python包名的命名:采用每个单词都是小写,不使用下划线 ...
- 如何手动写一个Python脚本自动爬取Bilibili小视频
如何手动写一个Python脚本自动爬取Bilibili小视频 国庆结束之余,某个不务正业的码农不好好干活,在B站瞎逛着,毕竟国庆嘛,还让不让人休息了诶-- 我身边的很多小伙伴们在朋友圈里面晒着出去游玩 ...
- 如何优雅的写好python代码?
Python与其他语言(比如 java或者 C ++ )相比有较大的区别,其中最大的特点就是非常简洁,如果按照其他语言的思路老师写Python代码,则会使得代码繁琐复杂,并且容易出现bug,在Pyth ...
- 手写算法-python代码实现KNN
原理解析 KNN-全称K-Nearest Neighbor,最近邻算法,可以做分类任务,也可以做回归任务,KNN是一种简单的机器学习方法,它没有传统意义上训练和学习过程,实现流程如下: 1.在训练数据 ...
- 【MaixPy3文档】写好 Python 代码!
本文是给有一点 Python 基础但还想进一步深入的同学,有经验的开发者建议跳过. 前言 上文讲述了如何认识开源项目和一些编程方法的介绍,这节主要来说说 Python 代码怎么写的一些演化过程和可以如 ...
- 我写的 Python 代码,同事都说好
原文链接: 我写的 Python 代码,同事都说好 人生苦短,我用 Python. 程序员的追求就是不写代码,早日财务自由.不对,一不小心把实话说出来了,应该是将代码写得简洁,优雅. Python 程 ...
- 写个Python练练手吧
在Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍. 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个.py文件, ...
- 为什么你写的Python运行的那么慢呢?
大约在一年前,也就是2013年在Waza(地名),Alex Gaynor提到了一个很好的话题:为什么用Python.Ruby和Javascript写的程序总是运行的很慢呢?正如他强调的,关键就是现在出 ...
随机推荐
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
- Juniper SSG5 PPTP VPN 619错误解决
公司分部的客户端需要使用PPTP VPN连接总部,将网关更换为Juniper SSG5后,客户端出现了每几个小时自动断开的现象,错误619. 解决:Security —— ALG —— 开启PPTP协 ...
- User mode Linux
一.简介 用户模式Linux(User ModeLinux,UML)不同于其他Linux虚拟化项目,UML尽量将它自己作为一个普通的程序.从Linux2.6.9版本起,用户模式Linux(User m ...
- python3.5批量修改文件名
考虑到要手工修改许多的文件名太费时,而且网上下载的图片大多如下(完全不能满足强迫症) 就用python来代替我们完成这个过程直接贴上代码 #encoding:utf-8 import os def r ...
- Azure机器学习入门(二)创建Azure机器学习工作区
我们将开始深入了解如何使用Azure机器学习的基本功能,帮助您开始迈向Azure机器学习的数据科学家之路. Azure ML Studio (Azure Machine Learning Studio ...
- easy_UI
引入js/css样式 <link rel="stylesheet" type="text/css" href="<%=path%>/ ...
- [No000097]程序员面试题集【下】
1.下面中共包含()个正方形?40 计算规律,设大长方形长是5,宽是4,小正方形边长是1,那么图中.小正方形的个数是,4*5个,边长是2的正方形的个数是3*4个,边长是3的正方形的个数是2*3个,边长 ...
- 内网劫持渗透新姿势:MITMf简要指南
声明:本文具有一定攻击性,仅作为技术交流和安全教学之用,不要用在除了搭建环境之外的环境. 0×01 题记 又是一年十月一,想到小伙伴们都纷纷出门旅游,皆有美酒佳人相伴,想到这里,不禁潸然泪下.子曰:& ...
- 设计模式之单例模式——Singleton
设计模式之单例模式--Singleton 设计意图: 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有 ...
- python基础补漏-06-内置模块
1> sys 模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的 ...