一、hashlib 生成MD5值
[[email protected] systeminformation]# vim hashlib2.py #!/usr/bin/env python import hashlib import sys def md5sum(f): m = hashlib.md5() with open(f) as fd: while True: data = fd.read(4096) if data: m.update(data) else: break return m.hexdigest() if __name__ == ‘__main__‘: try: print md5sum(sys.argv[1]) except IndexError: print "%s follow a argument" % __file__ [[email protected] systeminformation]# python hashlib2.py hashlib2.py follow a argument [[email protected] systeminformation]# python hashlib2.py /etc/passwd 8cb5df95a0685c814cfacd0fef10dc1c
二、walk模块
os.walk
迭代目录里的文件
[[email protected] systeminformation]# vim walk1.py #!/usr/bin/env python import hashlib import os import sys def md5sum(f): m = hashlib.md5() with open(f) as fd: while True: data = fd.read(4096) if data: m.update(data) else: break return m.hexdigest() a = os.walk(sys.argv[1]) for p,d,f in a: for i in f: fn = os.path.join(p,i) md5 = md5sum(fn) print md5+‘ ‘+fn [[email protected] systeminformation]# python walk1.py . 27f8b178ef14f5e79d4e875977c320f1 ./yield1.py 44ed2af7008a9e5bbd720495aaf07590 ./hashlib2.py c38e72d0b260e35efc2d32dc75a7a34e ./walk1.py d41d8cd98f00b204e9800998ecf8427e ./test/a d41d8cd98f00b204e9800998ecf8427e ./test/b
三、yield生成器
生成器是一个可迭代的对象,可以对可迭代对象进行遍历,比如字符串,列表等,都是可迭代对象
生成器对象
生成器是一个可迭代的对象,可以对可迭代对象进行遍历,比如字符串,列表等,都是可迭代对象
当使用for进行迭代的时候,函数内的代码才会被执行
mygenerator = (x*x for x in range(4))
next()方法
mygenerator.next()
[[email protected] systeminformation]# vim yield1.py #!/usr/bin/env python def h(): print ‘one‘ yield 1 print ‘two‘ yield 2 print ‘three‘ yield 3 a = h() ipython In [1]: def f(n): ...: for i in range(n): ...: yield i ...: In [11]: a Out[11]: <generator object f at 0x7fcb11732be0> In [4]: a.next() Out[4]: 0 In [5]: a.next() Out[5]: 1 In [6]: a.next() Out[6]: 2 In [7]: a.next() Out[7]: 3 In [8]: a.next() Out[8]: 4 In [9]: a.next() Out[9]: 5 In [10]: a.next() --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-10-aa817a57a973> in <module>() ----> 1 a.next() In [14]: a = f(5) In [15]: for i in a:print i 0 1 2 3 4
return与yield区别
return的时候这个函数的局部变量就都销毁了
所有return是得到所有结果之后的返回
yield是产生了一个可以恢复的函数(生成器),恢复了局部变量。
生成器只有在调用.next()时才运行函数生成一个结果
时间: 2024-10-23 02:17:26