推荐学习书目
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 · Oct 13, 2015 · 3770 views
    This topic created in 3869 days ago, the information mentioned may be changed or developed.
    每一次都清除原有文件内容,再写入新内容。以 w 方式打开文件只能在打开的时候覆盖原有内容。
    6 replies    2015-10-14 10:37:56 +08:00
    simon7
        1
    simon7  
       Oct 13, 2015 via iPhone
    w+
    heqingpan
        2
    heqingpan  
       Oct 13, 2015
    fobj=open("filename","a")
    fobj.write("msg")
    fobj.flush()
    fobj.close()
    SErHo
        3
    SErHo  
       Oct 13, 2015
    r+

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

    必须保证文件存在
    eightqueen
        4
    eightqueen  
    OP
       Oct 13, 2015
    @heqingpan 不想反复打开文件、关闭文件,我这个操作有可能重复多次。
    sNullp
        5
    sNullp  
       Oct 14, 2015
    seek()
    eightqueen
        6
    eightqueen  
    OP
       Oct 14, 2015
    @sNullp 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5582 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 01:23 · PVG 09:23 · LAX 18:23 · JFK 21:23
    ♥ Do have faith in what you're doing.