Python os.closerange() 方法

Python File(文件) 方法 Python OS 文件/目錄方法


概述

os.closerange() 方法用于關(guān)閉所有文件描述符 fd,從 fd_low (包含) 到 fd_high (不包含), 錯(cuò)誤會(huì)忽略。

語法

closerange()方法語法格式如下:

os.closerange(fd_low, fd_high);

參數(shù)

  • fd_low -- 最小文件描述符

  • fd_high -- 最大文件描述符

該方法類似于:

for fd in xrange(fd_low, fd_high):
    try:
        os.close(fd)
    except OSError:
        pass

返回值

該方法沒有返回值。

實(shí)例

以下實(shí)例演示了 closerange() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 打開文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 寫入字符串
os.write(fd, "This is test")

# 關(guān)閉文件
os.closerange( fd, fd)

print "關(guān)閉文件成功!!"

執(zhí)行以上程序輸出結(jié)果為:

關(guān)閉文件成功!!

Python File(文件) 方法 Python OS 文件/目錄方法