如何阅读.txt文件 [英] How to read .txt file
本文介绍了如何阅读.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要你的帮助。我有一个.txt文件,我想用Python 2.7打印所有文本。
I'm in need of your help. I have a .txt file and I want to print all the texts using Python 2.7.
我使用了这个代码,如下所示。但它没有显示任何文本,除了错误。
I've used this code which is shown below. But it is not showing any text except errors.
file = open('x.txt' , 'r')
print file.read
现在,该怎么办?
提前感谢你。
推荐答案
这样做:
file = open('x.txt', 'r')
file_contents = file.read()
print(file_contents)
file.close()
或者这个:
with open('x.txt', 'r') as file:
file_contents = file.read()
print(file_contents)
这篇关于如何阅读.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文