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
eightqueen
V2EX  ›  Python

python io 如何覆盖写?

  •  
  •   eightqueen · 2015-10-13 18:15:51 +08:00 · 3187 次点击
    这是一个创建于 3125 天前的主题,其中的信息可能已经有所发展或是发生改变。
    每一次都清除原有文件内容,再写入新内容。以 w 方式打开文件只能在打开的时候覆盖原有内容。
    6 条回复    2015-10-14 10:37:56 +08:00
    simon7
        1
    simon7  
       2015-10-13 18:55:32 +08:00 via iPhone
    w+
    heqingpan
        2
    heqingpan  
       2015-10-13 18:59:21 +08:00
    fobj=open("filename","a")
    fobj.write("msg")
    fobj.flush()
    fobj.close()
    SErHo
        3
    SErHo  
       2015-10-13 19:25:15 +08:00
    r+

    f.seek(0)
    f.write("...")
    f.truncate()

    必须保证文件存在
    eightqueen
        4
    eightqueen  
    OP
       2015-10-13 21:29:51 +08:00
    @heqingpan 不想反复打开文件、关闭文件,我这个操作有可能重复多次。
    sNullp
        5
    sNullp  
       2015-10-14 08:18:03 +08:00
    seek()
    eightqueen
        6
    eightqueen  
    OP
       2015-10-14 10:37:56 +08:00
    @sNullp 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2363 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:08 · PVG 23:08 · LAX 08:08 · JFK 11:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.