Python os.closerange() Method

Python OS File/Directory Methods


Overview

The os.closerange() method is used to close all file descriptors fd, from fd_low (inclusive) to fd_high (exclusive), errors are ignored.

Syntax

closerange()The syntax of the method is as follows:

os.closerange(fd_low, fd_high);

Parameters

  • fd_low-- minimum file descriptor

  • fd_high-- maximum file descriptor

This method is similar to:

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

Return Value

This method has no return value.

Example

The following example demonstrates the usage of the closerange() method:

#!/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")

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

print "关闭文件成功!!"

The output of the above program is:

File closed successfully!!

Python OS File/Directory Methods

Other Extensions