我是新來的類和我得到這個錯誤:錯誤「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)
沒有人知道爲什麼這不起作用?
它很明顯地告訴你,在例外中,'f2'被設置爲'None'。你的錯誤很可能在'readfile'的第三行 – Shadow
下面的人恭敬地回答。 – Brit