久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Python 文件對象上使用迭代器時是否需要 clos

Is close() necessary when using iterator on a Python file object(在 Python 文件對象上使用迭代器時是否需要 close())
本文介紹了在 Python 文件對象上使用迭代器時是否需要 close()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

執行以下操作并且顯式處理文件對象并調用其 close() 方法是不好的做法嗎?

Is it bad practice to do the following and not explicitly handle a file object and call its close() method?

for line in open('hello.txt'):
    print line

注意 - 這適用于還沒有 with 語句的 Python 版本.

NB - this is for versions of Python that do not yet have the with statement.

我問,因為 Python 文檔似乎建議這樣做:-

I ask as the Python documentation seems to recommend this :-

f = open("hello.txt")
try:
    for line in f:
        print line
finally:
    f.close()

這似乎比必要的更冗長.

Which seems more verbose than necessary.

推薦答案

在處理文件時,總是必須關閉,不要讓打開的文件句柄到處都是.當文件對象被垃圾回收時,它們最終將被關閉,但您不知道何時會關閉,同時您將通過持有不再需要的文件句柄來浪費系統資源.

Close is always necessary when dealing with files, it is not a good idea to leave open file handles all over the place. They will eventually be closed when the file object is garbage collected but you do not know when that will be and in the mean time you will be wasting system resources by holding to file handles you no longer need.

如果您使用的是 Python 2.5 及更高版本,則可以使用 with 語句自動為您調用 close():

If you are using Python 2.5 and higher the close() can be called for you automatically using the with statement:

from __future__ import with_statement # Only needed in Python 2.5
with open("hello.txt") as f:
    for line in f:
        print line

這與您的代碼具有相同的效果:

This is has the same effect as the code you have:

f = open("hello.txt")
try:
    for line in f:
        print line
finally:
    f.close()

with 語句是對 Resource Acquisition Is Initialization C++中常用的成語.它允許安全使用和清理各種資源,例如它可以用于始終確保關閉數據庫連接或始終釋放鎖,如下所示.

The with statement is direct language support for the Resource Acquisition Is Initialization idiom commonly used in C++. It allows the safe use and clean up of all sorts of resources, for example it can be used to always ensure that database connections are closed or locks are always released like below.

mylock = threading.Lock()
with mylock:
    pass # do some thread safe stuff

這篇關于在 Python 文件對象上使用迭代器時是否需要 close()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲一区二区三区在线免费观看 | 亚洲精品成人 | 国产精品一区三区 | 日韩精品在线一区 | 国产精品一区二区av | 欧美午夜视频 | 成人一区二区视频 | 国产成人精品一区二区三区四区 | 日韩av成人在线观看 | 国产日韩一区二区三免费高清 | 高清视频一区二区三区 | 成人精品久久 | 看av电影 | 亚洲成人av | 老牛影视av一区二区在线观看 | 草比网站| 亚洲国产日韩欧美 | 羞羞视频在线网站观看 | 日本a视频 | 99久久精品免费看国产四区 | www.色综合| 国产精品1区2区 | 国产探花在线观看视频 | 国产欧美在线播放 | av天空| 能看的av | 亚洲欧美日韩在线不卡 | 国产欧美日韩综合精品一区二区 | 一区在线视频 | 欧美一级在线免费观看 | 丝袜美腿一区 | 免费毛片在线 | 国产亚洲精品美女久久久久久久久久 | 国产一区二区三区久久久久久久久 | 不卡一区二区三区四区 | 九九亚洲精品 | 欧美自拍一区 | 狠狠爱网址 | 亚洲 欧美 另类 日韩 | 亚洲日韩中文字幕 | 九九视频在线观看视频6 |