It is good practice to use the with keyword when dealing with file
objects. The advantage is that the file is properly closed after its
suite finishes, even if an exception is raised at some point. Using
with is also much shorter than writing equivalent try-finally blocks:
将打印文件的内容
python中的文件具有读取、写入它们的方法
这里有一个很好的指南:
https://www.programiz.com/python-programming/file-operation
MT是一个对象,您应该使用它方法
read()
来显示文本因为} 。您可以在官方Python documentation中找到更多方法。另请注意:
MT
是实际的文件(一个TextIOWrapper
对象),而不是它的内容。您可以使用该对象的read
方法(即MT.read()
)或其他替代方法从该对象获取内容,具体取决于您的应用程序,例如我使用最多的^{相关问题 更多 >
编程相关推荐