OS File 中的 os.closerange(fd_low, f

首页 / Python2入门教程 / OS File 中的 os.closerange(fd_low, f

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!!

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

白话法律42讲 -〔周甲徳〕

软件工程之美 -〔宝玉〕

Python核心技术与实战 -〔景霄〕

网络编程实战 -〔盛延敏〕

Redis核心技术与实战 -〔蒋德钧〕

操作系统实战45讲 -〔彭东〕

Web漏洞挖掘实战 -〔王昊天〕

手把手教你落地DDD -〔钟敬〕

好记忆不如烂笔头。留下您的足迹吧 :)