V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dwjgwsm
V2EX  ›  Python

请问如何实现 open pickle 文件后先读后写?

  •  
  •   dwjgwsm · 2018-05-16 15:03:25 +08:00 · 1872 次点击
    这是一个创建于 2180 天前的主题,其中的信息可能已经有所发展或是发生改变。

    open rb+ 是可以先读后写,调试程序的时候才发现,不能在结合使用 pickle 的时候先读后写,写的时候会抛出错误.那么如何实现先读后 覆盖写 呢?

    一小段测试代码: http://chuantu.biz/t6/311/1526454143x-1404795577.jpg

    5 条回复    2018-05-18 18:06:12 +08:00
    jmc891205
        1
    jmc891205  
       2018-05-16 15:16:52 +08:00
    写到一个临时文件 确定写入成功之后把临时文件覆盖原文件
    dwjgwsm
        2
    dwjgwsm  
    OP
       2018-05-16 15:20:07 +08:00
    要这么操作才行啊
    dwjgwsm
        3
    dwjgwsm  
    OP
       2018-05-16 15:22:10 +08:00
    如果要这样,那还不如 open 二次 第一次读 第二次写
    lieh222
        4
    lieh222  
       2018-05-17 09:50:55 +08:00   ❤️ 1
    pickle.dump(fh)是啥意思,缺失参数吧,dump(obj, file, protocol=None)
    dwjgwsm
        5
    dwjgwsm  
    OP
       2018-05-18 18:06:12 +08:00
    @lieh222 原来是语法写错了!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2380 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 467ms · UTC 03:38 · PVG 11:38 · LAX 20:38 · JFK 23:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.