模块路径
- 可以定义PYTHONPATH环境变量,定义自己的模块位置
[root@localhost day02] mkdir /opt/pylibs
[root@localhost day02] cp qsort.py /opt/pylibs
[root@localhost day02] export PYTHONPATH=/opt/pylibs
[root@localhost day02] cd /tmp/
[root@localhost tmp] python3
>>> import qsort
[root@localhost day02]
[root@localhost day02]
[root@localhost day02]
hi = "Hello World"
[root@localhost day02]
>>> import pkgs.hello
>>> pkgs.hello.hi
'Hello World'
hashlib
>>> import hashlib
>>> m = hashlib.md5(b'123456')
>>> m.hexdigest()
'e10adc3949ba59abbe56e057f20f883e'
>>> m1 = hashlib.md5()
>>> m1.update(b'12')
>>> m1.update(b'34')
>>> m1.update(b'56')
>>> m1.hexdigest()
'e10adc3949ba59abbe56e057f20f883e'
import hashlib
import sys
def _hash(fn):
has = hashlib.md5()
with open(fn, 'rb') as fobj:
while 1:
con = fobj.read(4096)
if not con:
break
has.update(con)
return has.hexdigest()
if __name__ == '__main__':
print('%s %s' % (_hash(sys.argv[1]), sys.argv[1]))
tarfile
>>> import tarfile
>>> tar = tarfile.open('/tmp/mytest.tar.gz', 'w:gz')
>>> tar.add('/etc/hosts')
>>> tar.add('/etc/security')
>>> tar.close()
>>> tar = tarfile.open('/tmp/mytest.tar.gz')
>>> tar.extractall(path='/var/tmp')
>>> tar.close()