码迷,mamicode.com
首页 > 其他好文 > 详细

hashlib、walk、yield

时间:2016-10-25 20:11:16      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:hashlib、walk、yield

一、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

二、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()时才运行函数生成一个结果


本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1865354

hashlib、walk、yield

标签:hashlib、walk、yield

原文地址:http://daixuan.blog.51cto.com/5426657/1865354

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!