2017-10-16 64 views
0

我是新来的类和我得到这个错误:错误“NoneType”对象有没有属性“读”

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) 

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

+0

它很明显地告诉你,在例外中,'f2'被设置为'None'。你的错误很可能在'readfile'的第三行 – Shadow

+0

下面的人恭敬地回答。 – Brit

回答

1

这条线:

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

有一个错字。

+0

谢谢!当我试图在一个新函数中执行line_in_flights = self.f1.readline()时,我现在收到错误'NoneType'对象没有属性'截止日期',你知道为什么吗? – Brit

+0

新问题得到新问题。 –

+0

是什么?哦,但是,我必须等待一个小时是否正确? – Brit

相关问题