[英]Will `readlines()` close the opened file in python?
In Python, will在 Python 中,将
file_name = ''
content_list = open(file_name, 'r').readlines()
automatically close the opened file?自动关闭打开的文件?
No, you should use the Context Manager for it:不,您应该为此使用上下文管理器:
file_name = 'abc.txt'
with open(file_name, 'r') as txtFile:
content_list = txtFile.readlines()
No, you'd need to use with.不,你需要使用 with。 Either way, readlines will not close the file.
无论哪种方式, readlines 都不会关闭文件。
with open(file_name, 'r') as f:
content_list = f.readlines()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.