读取日志文件的标头(许多文件的通用标头)。创建了一个类并从另一个文件调用它的对象。标头类返回标头值的字典。
如何将整个文件作为参数从子类传递给头类?
儿童班:
class fileLog(object):
def __init__(self):
try:
# File path
filePath = "C:\FileLog1.log"
file = open(filePath, "rb")
with file:
self.fetchHeader(file)
finally:
file.close()
# This method fetches the header information
def getHeader(self, file):
'''
Get header information from header class
'''
header = HeaderInfo(file)
header_dict = header.Fetch_Data()
....
....
头信息类:
class HeaderInfo(dict):
def __init__(self, fileName):
header_dict = defaultdict(list)
print(type(fileName))
# self._object = self.file
self._fileName = fileName
file = open(self._fileName, "rb")
self._dict = {}
....
....
def Get_Data(self):
return self._dict
hi = HeaderInfo('**** what to pass here ? ****')
if __name__ == '__main__':
hi.Fetch_Data('** what to pass here ? **')
我不想在头信息类中硬编码文件名,因为多个文件可以使用同一个类并且必须动态传递文件。
错误 :
TypeError:init()缺少1个必需的位置参数:'fileName'
我知道我没有传递文件名,但是如何处理 HeaderInfo 类中的文件名?