您的位置:首页 > 其它

hashlib、walk、yield

2016-10-25 11:52 190 查看
一、hashlib 生成MD5值

[root@133 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__

[root@133 systeminformation]# python hashlib2.py
hashlib2.py follow a argument
[root@133 systeminformation]# python hashlib2.py /etc/passwd
8cb5df95a0685c814cfacd0fef10dc1c
小试牛刀
一个文件全部是手机号码,每行一个,需要将该文件的每个手机号码md5处理
[root@133 systeminformation]# vim 1_md5.py
#!/usr/bin/env python
#coding=utf-8
import hashlib
import sys
import os.path

def  md5sum(f):
f_out = open('/tmp/out.txt','w')
with open(f) as fd:
for i  in  fd.readlines():
line = i.strip('\n')
md = hashlib.md5(line)
f_out.write(md.hexdigest()+'\n')

if __name__ == '__main__':
try:
print md5sum(sys.argv[1])
except IndexError:
print "%s follow a argument" % __file__

[root@133 systeminformation]# python 1_md5.py 非码+支付宝会员_喜欢牛肉_手机号码.txt
None
[root@133 systeminformation]# head /tmp/out.txt
1ed73d40234d99ef1ecfb00264af98e8
e85faa993080d67c5f791fdc015ad3d1
45a0515b1311341ae50c518999ba56e3
254d3e8963dc301e2d06a4fbacae6b76
5861016350aa8a9e2242113d40f0c5c5
aea768a8aac8c1e9ce74f7e9bb0a4a2b
34a5fdef10ea2c4d3300848837f05633
2e3fa16b751d1df711f6271554c8074c
caad46ce2ad1287ea8a60dd13e9314f4
d11598a9f78731f6911a1902990e8932


二、walk模块

os.walk
迭代目录里的文件
[root@133 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
[root@133 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()
[root@133 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()时才运行函数生成一个结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  yield walk hashlib