摘要:
使用正则表达式收集主机信息 #!/usr/bin/env python from subprocess import Popen, PIPE import re def getIfconfig(): p = Popen(['ifconfig'], stdout=PIPE) data = p.stdo 阅读全文
摘要:
#!/usr/bin/env python from subprocess import Popen, PIPE def getIfconfig(): p = Popen(['ifconfig'], stdout=PIPE) data = p.stdout.read() return data de 阅读全文
摘要:
#!/usr/bin/env python from subprocess import Popen, PIPE def getIfconfig(): p = Popen(['ifconfig'], stdout=PIPE) data = p.stdout.read().split('\n\n') 阅读全文
摘要:
#!/usr/bin/env python from subprocess import Popen, PIPE p = Popen(['dmidecode'], stdout=PIPE) data = p.stdout lines = [] dmi = {} a = True while a: l 阅读全文
摘要:
按字典值排序 按照字典value排序,类似sort -k 命令 import operator x= {1:2,3:4,4:3,2:1,0:0} sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1)) In [38]: sorted_ 阅读全文
摘要:
#!/usr/bin/env python import sys import hashlib def md5sum(f): m = hashlib.md5() with open(f) as fd: while True: data = fd.read(4096) if data: m.updat 阅读全文
摘要:
#!/usr/bin/python import sys import os try: fn = sys.argv[1] except IndexError: print "please follow a argument at %s" % __file__ sys.exit() if not os 阅读全文
摘要:
vim wc.py #!/usr/bin/python def wordCount(s): chars = len(s) words = len(s.split()) lines = s.count('\n') print lines, words, chars if __name__ == '__ 阅读全文
摘要:
Python 函数 lambda 匿名函数 -lambda 函数是一种快速定义单行的最小函数,可以用在任何需要函数的地方。 def fun(x,y): return x*y fun(2,3) r=lambda x,y:x*y r(2,3) In [10]: def fun(x,y): ....: r 阅读全文
摘要:
#!/usr/bin/python import os import sys def print_files(path): lsdir = os.listdir(path) dirs = [i for i in lsdir if os.path.isdir(os.path.join(path,i)) 阅读全文
摘要:
计算阶层 普通方法: -使用循环 #!/usr/bin/python def factorial(n): sum = 1 for i in range(1,n+1): sum *= i return sum print factorial(5) 计算阶层 python 7.py 120 #!/usr 阅读全文