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的更多相关文章

  1. 将自己写的Python代码打包放到PyPI上

    如果是开源的Python代码,为了能够让大家更方便的使用,放到PyPI上也许是个非常不错的主意(PyPI:Python Package Index).刚开始我以为要将代码打包放到PyPI上是一件非常复 ...

  2. 写一个python的服务监控程序

    写一个python的服务监控程序 前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4. 到python网站下载源代码,解压到Redhat上, ...

  3. 我写的python代码的规则

    1.Python文件的命名: 采用每个单词的首字母大写,不使用下划线 2.Python类的命名: 采用每个单词的首字母大写,不使用下划线 3.Python包名的命名:采用每个单词都是小写,不使用下划线 ...

  4. 如何手动写一个Python脚本自动爬取Bilibili小视频

    如何手动写一个Python脚本自动爬取Bilibili小视频 国庆结束之余,某个不务正业的码农不好好干活,在B站瞎逛着,毕竟国庆嘛,还让不让人休息了诶-- 我身边的很多小伙伴们在朋友圈里面晒着出去游玩 ...

  5. 如何优雅的写好python代码?

    Python与其他语言(比如 java或者 C ++ )相比有较大的区别,其中最大的特点就是非常简洁,如果按照其他语言的思路老师写Python代码,则会使得代码繁琐复杂,并且容易出现bug,在Pyth ...

  6. 手写算法-python代码实现KNN

    原理解析 KNN-全称K-Nearest Neighbor,最近邻算法,可以做分类任务,也可以做回归任务,KNN是一种简单的机器学习方法,它没有传统意义上训练和学习过程,实现流程如下: 1.在训练数据 ...

  7. 【MaixPy3文档】写好 Python 代码!

    本文是给有一点 Python 基础但还想进一步深入的同学,有经验的开发者建议跳过. 前言 上文讲述了如何认识开源项目和一些编程方法的介绍,这节主要来说说 Python 代码怎么写的一些演化过程和可以如 ...

  8. 我写的 Python 代码,同事都说好

    原文链接: 我写的 Python 代码,同事都说好 人生苦短,我用 Python. 程序员的追求就是不写代码,早日财务自由.不对,一不小心把实话说出来了,应该是将代码写得简洁,优雅. Python 程 ...

  9. 写个Python练练手吧

    在Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍. 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个.py文件, ...

  10. 为什么你写的Python运行的那么慢呢?

    大约在一年前,也就是2013年在Waza(地名),Alex Gaynor提到了一个很好的话题:为什么用Python.Ruby和Javascript写的程序总是运行的很慢呢?正如他强调的,关键就是现在出 ...

随机推荐

  1. Elasticsearch 调优 (官方文档How To)

    How To Elasticsearch默认是提供了一个非常简单的即开即用体验.用户无需修改什么配置就可以直接使用全文检索.结果高亮.聚合.索引功能. 但是想在项目中使用高性能的Elasticsear ...

  2. cookie 跨域访问的解决方案

    Cookie 同域单点登录  最近在做一个单点登录的系统整合项目,之前我们使用控件实现单点登录(以后可以介绍一下).但现在为了满足客户需求,在不使用控件情况下实现单点登录,先来介绍一下单点登录.    ...

  3. MMORPG大型游戏设计与开发(服务器 AI 逻辑设定和状态结点)

    人工智能(AI)中往往都会有这么一个问题,那就是我要做什么?我该怎么做?我需要什么?所以这里所谓的智能就是赋予AI对象的判断力,以及它根据判断得到的相应反应.就好比,你去商店买东西,钱够别人才卖给你, ...

  4. 图像处理中任意核卷积(matlab中conv2函数)的快速实现。

    卷积其实是图像处理中最基本的操作,我们常见的一些算法比如:均值模糊.高斯模糊.锐化.Sobel.拉普拉斯.prewitt边缘检测等等一些和领域相关的算法,都可以通过卷积算法实现.只不过由于这些算法的卷 ...

  5. BZOJ 2693: jzptab [莫比乌斯反演 线性筛]

    2693: jzptab Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1194  Solved: 455[Submit][Status][Discu ...

  6. Everything(文件搜索神器)

    前言 Everything官网: http://www.voidtools.com/ 软件版本: V1.3.4.686 (x64) 操作系统: windows 7/10 搜索FTP(内网)资源 比如内 ...

  7. python 第一章学习课程

    http://www.runoob.com/python/python-dictionary.html

  8. [原]CentOS7 部署GeoServer2.92

    转载请注明作者think8848和出处(http://think8848.cnblogs.com) 1. 安装Jre 1. 安装ftp客户端 sudo yum install ftp -y 2. 登录 ...

  9. #define与typedef在重定义类型中的区别

    #define 为完全的替换 typedef 重新定一个数据类型 eg #define charp1 char* typedef char* charp2charp1 a,b; //a char* b ...

  10. iOS学习-UIButton的imageView和titleLabel

    UIButton的imageView和titleLabel的位置设置通过setImageEdgeInsets和setTitleEdgeInsets来设置 参考:http://blog.csdn.net ...