Python os.closerange() method
Python OS file/directory methods
Overview
The os.closerange() method closes all file descriptors fd from fd_low (inclusive) to fd_high (exclusive); errors are ignored.
Syntax
The **closerange()** method syntax is as follows:
os.closerange(fd_low, fd_high);
Parameters
-
fd_low -- The smallest file descriptor
-
fd_high -- The largest 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 use of the closerange() method:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# Open file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write string
os.write(fd, "This is test")
# Close file
os.closerange( fd, fd)
print "关闭文件成功!!"
The output of the above program is:
关闭文件成功!!