语法
以下是 closerange() 方法的语法:
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
返回值
此方法不返回任何值。
示例
下面的示例演示 closerange() 方法的使用。
#!/usr/bin/python3
import os, sys
# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write one string
line="this is test"
# string needs to be converted byte object
b=str.encode(line)
os.write(fd, b)
# Close a single opened file
os.closerange( fd, fd)
print ("Closed all the files successfully!!")
这将创建指定的文件 foo.txt,然后按给出内容写入该文件。这将产生以下结果:
Closed all the files successfully!!