【问题标题】:Python cause of "AttributeError: 'NoneType' object has no attribute 'readline'"“AttributeError:'NoneType'对象没有属性'readline'”的Python原因
【发布时间】:2021-12-08 15:42:24
【问题描述】:

我正在尝试从每行有 1 个整数的文件中打印整数,并打印它们的总和。一切似乎都工作正常,除非我输入了错误的文件名,循环回来,然后输入正确的文件名。该程序仍然输出正确的信息,但随后出现错误: “AttributeError:‘NoneType’对象没有属性‘readline’”。为什么会这样?

def main():
    listnums = filetolist()
    print(f'numbers in file: {listnums}')
    print(f' sum of before mentioned numbers is: {sum(listnums)}')


# opens file
def openfile():
    try:
        file = open(input("what is the file named including the file extension?"), "r")
        return file
    except IOError:
        tryagain = input("File could not be found \n" + "if you would like try again type 1 and press enter, to exit press enter")
        if tryagain == "1":
            main()
        else:
            exit()


# converts file to list
def filetolist():
    file = openfile()
    line = file.readline()
    nums = []
    linenumber = 1
    while line != "":
        nums += [verifyint(line, linenumber)]
        line = file.readline()
        linenumber += 1
    file.close()
    return nums


# verifies number is an int
def verifyint(num, linenumber):
    try:
        numint = int(num)
        return numint
    except ValueError:
        print(f'Invalid value on line #{linenumber}')
        exit()


main()

【问题讨论】:

    标签: python readline attributeerror try-except


    【解决方案1】:

    当你点击except块时,没有return语句,所以这个函数在再次运行main()后返回None

    您应该正确地引发错误并使用适当的循环,而不是有效地递归

    def filetolist(filename):
        with open(filename) as f:
            return [int(line.rstrip()) for line in f]
    
    def main():
        while True:
            filename = input("what is the file named including the file extension?")
            try:
                listnums = filetolist(filename)
                print(f'numbers in file: {listnums}')
                print(f' sum of before mentioned numbers is: {sum(listnums)}')
            except ValueError, IOError:
                again = input('Error! Try again? (y/n)')
                if again.lower() != 'y':
                    break
    
    main()
    

    【讨论】:

      【解决方案2】:

      我认为(我可能错了)问题在于,当您无法读取文件并重试时,您需要调用openfile 而不是整个main。 如果您调用整个main,您将打开一个新文件,执行所有例程,然后什么也不返回。

      试一试,告诉我它是否有效

      def openfile():
          try:
              file = open(input("what is the file named including the file extension?"), "r")
              return file
          except IOError:
              tryagain = input("File could not be found \n" + "if you would like try again type 1 and press enter, to exit press enter")
              if tryagain == "1":
                  return openfile()
              else:
                  exit()
      

      这里是截图

      【讨论】:

      • 它对我有用。我要编辑答案以添加屏幕截图
      • 使用 return openfile() 工作,谢谢
      猜你喜欢
      • 2017-12-28
      • 2017-10-05
      • 2018-03-17
      • 2019-01-10
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      相关资源
      最近更新 更多