易百教程

41、打开文本文件并显示其内容的最短方法是什么?

打开文本文件的最短方法是使用with命令,方法如下:

例子:

with open(r"c:\Users\Administrator\Desktop\dofile.txt", "r") as fp:  
    fileData = fp.read()    
# To print the contents of the file     
print(fileData)

运行结果如下:

Life is short,you need Python.