要快速读取文件,我可以这样做:
with open('foo', 'r') as fd:
content = fd.read()或
content = open('foo').read()在这里使用with语句有什么好处吗?
发布于 2016-12-01 10:39:09
第一种方法确保文件无论如何都将被关闭。就像在做:
try:
fd = open('foo')
content = fd.read()
# ... do stuff here
finally:
fd.close()https://stackoverflow.com/questions/40908338
复制相似问题