-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos_closerange.py
More file actions
45 lines (34 loc) · 948 Bytes
/
os_closerange.py
File metadata and controls
45 lines (34 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-03-24 09:57:39
# @copyright by hoojo@2018
# @changelog Added python3 `os file -> closerange` example
'''
概述
os.closerange() 方法用于关闭所有文件描述符 fd,从 fd_low (包含) 到 fd_high (不包含), 错误会忽略。
语法
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
返回值
该方法没有返回值
'''
import os
# 打开文件
fd = os.open("/tmp/foo.txt", os.O_RDWR|os.O_CREAT)
# 写入字符串
os.write(fd, "This is test2")
# 关闭文件
os.closerange(fd, fd)
print("关闭文件成功!!")