import os defsearch_str(file, key): try: ifnot os.path.exists(file): raise FileNotFoundException() if key == '': raise KeyEmptyException() withopen(file, mode='r', encoding='utf8') as f: for line in f: if line.startswith(key): print('找到了,',line) break else: raise ContentNotFoundException()
except FileNotFoundException as e: print(e.code, e.msg) except ContentNotFoundException as e: print(e.code, e.msg) except KeyEmptyException as e: print(e.code, e.msg) except Exception as e: print(e) search_str('aaa.text', 'ddd')
python MD5加密:
python中md5加密需要引入一个模块hashlib
1 2 3 4 5 6
hashlib obj = hashlib.md5() obj.update(.encode()) v = obj.hexdigest()