0

读取日志文件的标头(许多文件的通用标头)。创建了一个类并从另一个文件调用它的对象。标头类返回标头值的字典。

如何将整个文件作为参数从子类传递给头类?

儿童班:

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 类中的文件名

4

1 回答 1

0
hi = HeaderInfo('pass file name here')

实际上错误表明,init方法有文件名作为参数,如果你像hi = HeaderInfo()一样调用,它会抛出像TypeError这样的错误:init()缺少1个必需的位置参数:'fileName',所以在创建时传递文件名类的对象

于 2020-02-24T03:43:32.413 回答