错误' NoneType'对象没有属性' read'

时间:2017-10-16 00:43:24

标签: python python-3.x

我是新上课的,我收到了这个错误:

   self.a2=self.f2.read()
AttributeError: 'NoneType' object has no attribute 'read'

这是我的代码:

class flights(object):
    """
            A class that can read in files about flights and report that information
    """
    def __init__(self, filename1,filename2):
        """when typing in the filename1 and filename2 make
        sure to put them is as "filename.txt"
        """
        self.filename1=filename1
        self.filename2=filename2
        self.f1=None# none is a place holder
        self.f2=None
        self.a1=None
    def showx(self):
        print(self.filename1)
        print(self.filename2)
    def readfile(self):
        self.f1=open(self.filename1,'r')
        self.a1=self.f1.read()
        self.f1=open(self.filename2,'r')
        self.a2=self.f2.read()
        print(self.a1)
        print(self.a2)

有谁知道为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

这一行:

  self.f1=open(self.filename2,'r')

有一个错字。