Python 3.5 编写一个程序,每次读取一行hoilday.txt的内容

Python 3.5 编写一个程序,每次读取一行hoilday.txt的内容,python-3.5,Python 3.5,我必须在python 3.5.2上完成这个编码挑战 到目前为止,我的代码如下: file = open("holiday text",'r') contents = file.read print(contents) file.close() 这应该能奏效。请注意,如果文本文件与python不在同一文件夹中,例如C:/Python35-32,则应指定整个路径,除非是为了某个在线挑战而提供文本文件 file = open("holiday text.txt",'r') contents = fi

我必须在python 3.5.2上完成这个编码挑战

到目前为止,我的代码如下:

file = open("holiday text",'r')
contents = file.read
print(contents)
file.close()

这应该能奏效。请注意,如果文本文件与python不在同一文件夹中,例如C:/Python35-32,则应指定整个路径,除非是为了某个在线挑战而提供文本文件

file = open("holiday text.txt",'r')
contents = file.read()
file.close()
print(contents)
另一种方法是使用with语句,它会自动适当地打开/关闭文件,如下所示:

with open("holiday text.txt",'r') as file:
    contents = file.read()
print(contents)
如果有帮助,请按箭头按钮获取已接受的答案