Latest web development tutorials

Python os.closerange () method

Python File (File) method Python OS file / directory methods


Outline

os.closerange () method is used to close all file descriptors fd, from fd_low (inclusion) to fd_high (not included), the error will be ignored.

grammar

closerange () method syntax is as follows:

os.closerange(fd_low, fd_high);

parameter

  • fd_low - the smallest file descriptor

  • fd_high - Maximum file descriptor

This method is similar:

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

return value

This method has no return value.

Examples

The following example demonstrates the use of 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 above program output is:

关闭文件成功!!

Python File (File) method Python OS file / directory methods