【问题标题】:write a program that reads the content of hoilday.txt, one line at a time编写一个程序,读取holiday.text的内容,一次一行
【发布时间】:2017-02-09 20:21:02
【问题描述】:

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

到目前为止,这是我的代码:

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

【问题讨论】:

    标签: python-3.5


    【解决方案1】:

    这应该可以解决问题。请注意,如果文本文件与 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)
    

    如果有帮助,请按箭头按钮接受答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多