每天写点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基本知识0
linux的基本原则: 1.有目的单一的小程序组成,组合小程序完成复杂任务. 2.一切皆文件 3.尽量避免捕获用户接口 4.配置文件保存为纯文本格式 CLI接口: 命令提示符,prompt,bash ...
- HTTP Method 之 Post VS. Get
引言 WebAPI 现在非常火的轻量服务框架,因其使用得使用了Http协议,并且具备了可协商内容(生成不同内容格式)等优势,所以在互联网业务中被广泛使用. 那作为HTTP最常用的两个方法Get和Pos ...
- github page+jekyll搭博客初体验
div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin: 0 0 10px; padding ...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...
- angular学习笔记(二十八-附1)-$resource中的资源的方法
通过$resource获取到的资源,或者是通过$resource实例化的资源,资源本身就拥有了一些方法,$save,$delete,$remove,可以直接调用来保存该资源: 比如有一个$resour ...
- lydsy 2600(二分+中位数前缀和)米仓
2600: [Ioi2011]ricehub Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 691 Solved: 359[Submit][Stat ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- 数组为什么可以使用linq查询
问题引出 这视乎是个完全不必要进行讨论的话题,因为linq(这里具体是linq to objects)本来就是针对集合类型的,数组类型作为集合类型的一种当然可以使用了.不过我还是想写一下,这个问题源于 ...
- 【MySQL】花10分钟阅读下MySQL数据库优化总结
1.花10分钟阅读下MySQL数据库优化总结http://www.kuqin.com2.扩展阅读:数据库三范式http://www.cnblogs.com3.my.ini--->C:\Progr ...
- SQLAlchemy(一)
说明 SQLAlchemy只是一个翻译的过程,我们通过类来操作数据库,他会将我们的对应数据转换成SQL语句. 运用ORM创建表 #!/usr/bin/env python #! -*- coding: ...