Bug 351

Summary: windows系统不兼容,文件空报错
Product: EnglishPal Reporter: 戴哲渊 <290194673>
Component: Bug报告Assignee: Hui Lan <lanhui>
Status: CONFIRMED ---    
Severity: enhancement    
Priority: ---    
Version: 0.1   
Hardware: PC   
OS: Windows   

Description 戴哲渊 2021-11-25 19:19:14 CST
EnglishiPal项目
1.当用户创建新账号时,由于windows系统不兼容原来创建新文件语句,导致创建文件失败。
出现代码位置:main.py  load_freq_history()函数
windows端修改
def load_freq_history(path):
    d = {}
    if os.path.exists(path):
        d = pickle_idea.load_record(path)
    else:
        # windows不支持
        # os.mknod(path)
        f = open(path,'w')
        f.close()
    return d
2.当.pickle文件为空时,打开会报EOF错
出现代码位置:pickle_idea.py  load_record()函数
修改建议
def load_record(pickle_fname):
    # 旧写法可能会出现EOFError
    # f = open(pickle_fname, 'rb')
    # d = pickle.load(f)
    # f.close()
    # return d
    # 新写法,也可以使用try/catch捕获
    if os.path.getsize(pickle_fname) > 0:
        with open(pickle_fname, 'rb+') as f:
            unpickler = pickle.Unpickler(f)
            d = unpickler.load()
        return d
    return ''
Comment 1 Hui Lan 2021-11-28 22:13:33 CST
    else:
        # windows不支持
        # os.mknod(path)
        f = open(path,'w')
        f.close()


不懂上面语句起什么作用?新建一个空文件?

Hui
Comment 2 Hui Lan 2021-11-28 22:17:15 CST
> unpickler = pickle.Unpickler(f)

为什么不直接用pickle.load(f)?

Hui