-2
我必须在python 3.5.2
上做这个编码挑战。写一个程序,读取hoilday.txt的内容,一次一行
到目前为止,这里是我的代码:
file = open("holiday text",'r')
contents = file.read
print(contents)
file.close()
我必须在python 3.5.2
上做这个编码挑战。写一个程序,读取hoilday.txt的内容,一次一行
到目前为止,这里是我的代码:
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)
如果它帮助,请按为接受的答案的箭头按钮。