在一行代码中打开、读取和关闭文件。

10 浏览
0 Comments

在一行代码中打开、读取和关闭文件。

现在我使用的是:

pageHeadSectionFile = open('pagehead.section.htm','r')
output = pageHeadSectionFile.read()
pageHeadSectionFile.close()

但为了使代码更美观,我可以这样做:

output = open('pagehead.section.htm','r').read()

当使用上述语法时,我如何关闭文件以释放系统资源?

0