문제

Problem is I can read only the first line of the the InputFile.bak file. How I can read all the information from the file using cPickle.

Input file-InputFile.bak

 (dp1
S'Here we go'
p2
(cdatetime
date
p3
(S'\x07\xdc\x0c\x0c'
tRp4
cdatetime
time
p5
(S'\x0c\x0c\x00\x00\x00\x00'
tRp6
tp7
s.(dp1
S'Here we go'
p2
(cdatetime
date
p3
(S'\x07\xdc\x0c\x0c'
tRp4
cdatetime
time
p5
(S'\x0c\x0c\x00\x00\x00\x00'
tRp6
tp7
s.(dp1
S'Here we go'
p2
(cdatetime
date
p3
(S'\x07\xdc\x0c\x0c'
tRp4
cdatetime
time
p5
(S'\x0c\x0c\x00\x00\x00\x00'
tRp6
tp7
sS'Google Searching'
p8
(g3
(S'\x07\xdc\x0c\x0b'
tRp9
g5
(S'\x01\x17\x00\x00\x00\x00'
tRp10
tp11
s.

Source Code

import time
import datetime
import cPickle
import os
from sys import exit


def read_file():
    if os.path.exists('InputFile.bak'):
        try:
            fname = open('InputFile.bak', 'rb')
            file_src = cPickle.Unpickler(fname)
            item_name = file_src.load()
            for k, v in item_name.iteritems():
                print v[0], "\t", v[1],"\t", k
        finally:
            fname.close()
    else:
        item_name = {}

if __name__ == '__main__':
    read_file()

Thank you very much.

도움이 되었습니까?

해결책

You can use loop to get all records.

def read_file():
    if os.path.exists('InputFile.bak'):
        # try:
        with open('InputFile.bak', 'rb') as fname:
            while True:
                try:
                    item_name = cPickle.load(fname)
                    for k, v in item_name.iteritems():
                        print v[0], "\t", v[1],"\t", k
                except EOFError:
                    break
    else:
        item_name = {}

if __name__ == '__main__':
    read_file()

다른 팁

If you know that another process will not be adding to the file while you are reading it, you can check the current progress against the file size:

def read_file():
    fname = 'InputFile.bak'
    if os.path.exists(fname):
        fsize = os.path.getsize(fname)
        with open(fname, 'rb') as fh:
            while fh.tell() < fsize:
                item = cPickle.load(fh)
                for k, v in item.iteritems():
                    print v[0], "\t", v[1],"\t", k
    else:
        item_name = {}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top