Python os.closerange() 方法

返回上一级

os.closerange() 方法用于关闭所有文件描述符 fd

从 fd_low (包含) 到 fd_high (不包含), 错误会忽略

导入模块

import os

语法

os.closerange(fd_low, fd_high);

参数

参数 说明
fd_low 最小文件描述符
fd_high 最大文件描述符

说明

该方法的实现类似于

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

返回值

范例

下面的代码使用 os.closerange() 方法关闭一序列的文件描述符

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

import os, sys

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

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

# 关闭文件
os.closerange( fd, fd)

print "关闭文件成功!!"

运行以上 Python 代码,输出结果如下

关闭文件成功!!

返回上一级

Python2 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.