推荐学习书目
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
oldbird
V2EX  ›  Python

py2 中写入文本可以不 close 只 flush 吗?

  •  
  •   oldbird · Feb 7, 2021 · 2566 views
    This topic created in 1919 days ago, the information mentioned may be changed or developed.

    不调用 close 会不会有影响?

    5 replies    2021-02-13 13:55:03 +08:00
    alazysun
        1
    alazysun  
       Feb 7, 2021
    讲道理,其实是没什么影响的,你是不想手动调用 close 吗?建议使用 with as
    learningman
        2
    learningman  
       Feb 7, 2021
    上下文管理器嘛,就楼上说的 with open as xxx
    Macv1994
        3
    Macv1994  
       Feb 7, 2021
    直接使用 with 不需要你手动 close
    Yinz
        4
    Yinz  
       Feb 9, 2021   ❤️ 1
    不主动 close 的话,在你的进程退出之前会一直占用文件描述符,如果占用太多会导致其他程序无法申请文件描述符与。
    但是你开的文件数量少的话没影响
    northisland
        5
    northisland  
       Feb 13, 2021   ❤️ 1
    如果需要不停处理几千几万个文件,需要。

    不关闭,会有文件处理进程留下来,等进程池发不出来资源了,机器就异常了。(以前训练服务器遇到莽壮小伙,出过这个问题。)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3779 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 04:54 · PVG 12:54 · LAX 21:54 · JFK 00:54
    ♥ Do have faith in what you're doing.