withopen('filename.txt', 'r') asdatafile:for line indatafile: print(line)datafile = open('filename.txt', 'r')for line in datafile:print(line)datafile.close()from nettools import NetworkElementwith NetworkElement('171.0.2.45') as ne:for route in ne.routing_table:print'%15s -> %s' % (route.name, route.ipaddr)classNetworkElement(object)def__enter__(self):returnselfdef__exit__(self, exctype, excinst, exctb):if exctype == NetworkElementError: logging.exception('No routing table found')self.oldne.cleanup('rollback')else:self.oldne.cleanup('commit')self.oldne.disconnect()import pytestdeftest_zero_division():with pytest.raises(ZeroDivisionError):1 / 0from unittest.mock import patchwith patch('Class.method', mocked_method): # 运行测试代码 长按或扫描下方二维码,免费获取 Python公开课和大佬打包整理的几百G的学习资料,内容包含但不限于Python电子书、教程、项目接单、源码等等 推荐阅读
点击 阅读原文 了解更多