无涯教程-OS File - os.closerange(fd_low, fd_high)函数

66 阅读1分钟

Python方法closerange()关闭从FD_LOW(包括)到FD_HIGH(独占)的所有文件描述符,忽略错误。此方法是在Python 2.6版中引入的。

os.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

os.closerange - 示例

以下示例显示closerange()方法的用法。

#!/usr/bin/python

import os, sys

# Open a file fd=os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string os.write(fd, "This is test")

# Close a single opened file os.closerange( fd, fd)

print "Closed all the files successfully!!"

这将创建给定文件foo.txt,然后将给定内容写入该文件。这将产生以下输出-

Closed all the files successfully!!

参考链接

www.learnfk.com/python/os-c…