pickle文件存储和保存

import pickle


class Cat:
    '''
    The object of cat
    '''
    foot = "foot"
    head = "head"
    body = "body"


def open_pickle_file(file_path):
    '''
    Open the file of bin
    :param file_path: path of file
    :return:
    '''
    with open(file_path, 'rb') as f:
        data = pickle.load(f)
        print(data[0].head)
        print(data[0].body)
        print(data[0].foot)
    return 0


def save_pickle_file(data, file_path):
    '''
    Save the file of data
    :param data: The data of input
    :param file_path: The path of file
    :return:
    '''
    with open(file_path, 'wb') as f:
        pickle.dump(data, f)


if __name__ == '__main__':
    file_path = 'D:/tmp.pk'
    data = list()
    data.append(Cat())
    data.append(Cat())
    save_pickle_file(data, file_path)
    open_pickle_file(file_path)

猜你喜欢

转载自blog.csdn.net/make_progress/article/details/82178521
今日推荐