每天写点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写的程序总是运行的很慢呢?正如他强调的,关键就是现在出 ...
随机推荐
- qt中ui的 使用介绍
1.什么是ui?ui通常是用Qt 设计师设计出来的界面文件的后缀.通常情况下ui是一个指向这个界面类的指针.ui-> 一般就是用来访问这个界面类里面的控件.例如你的ui文件里有一个叫okButt ...
- Freeswitch 添加可转码的G729编码
默认情况下Freeswitch自带的G729模块是pass-through-并不支持转码. 不过我们依然有个好奇的心, 所以我们决定添加一个支持G729转码的模块到Freeswitch. 1. 下载m ...
- Java To CSharp源代码转换
前言 开发环境 客户端:Unity3D开发(C#) 服务器:Java (基于Java7) 日 期:2016年09月 需求说明 部分服务器的部分逻辑功能在客户端实现一遍,可以简单的理解为服务器的部分 ...
- TCP/IP协议(二)tcp/ip基础知识
今天凌晨时候看书,突然想到一个问题:怎样做到持续学习?然后得出这样一个结论:放弃不必要的社交,控制欲望,克服懒惰... 然后又有了新的问题:学习效率时高时低,状态不好怎么解决?这也是我最近在思考的问题 ...
- 【IScroll深入学习】解决IScroll疑难杂症
前言 在去年,我们对IScroll的源码进行了学习,并且分离出了一段代码自己使用,在使用学习过程中发现几个致命问题: ① 光标移位 ② 文本框找不到(先让文本框获取焦点,再滑动一下,输入文字便可重现) ...
- 在ASP.NET Core中使用Angular2,以及与Angular2的Token base身份认证
注:下载本文提到的完整代码示例请访问:How to authorization Angular 2 app with asp.net core web api 在ASP.NET Core中使用Angu ...
- quartus和modelsim中使用mif和hex文件
.mif和.hex文件都是Quartus支持的数据文件格式,常被用作内存初始化文件.可是,M odelSim却不支持.mif文件,只支持.hex文件格式,这意味着如果你的设计采用了.mif文件 ,你的 ...
- 【VS】vs修改大小写快捷键
选中一段英文 改成小写:Ctrl+U 改成大写:Ctrl+Shift+U
- 如何使用PullToRefresh
这里有详解 PullToRefresh使用详解(一)--构建下拉刷新的listView PullToRefresh使用详解(二)---重写BaseAdapter实现复杂XML下拉刷新 PullToRe ...
- Mac合并分区
Mac合并分区 公司Mac电脑之前是win系统,重新安装了mac系统后,多了一个分区.用分区助手打开后,减号是灰色的,上网搜了很多帖子,都是点击减号直接将分区删除等等.减号不能点击的就要重新装系统了. ...