-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos_dup2.py
More file actions
48 lines (36 loc) · 892 Bytes
/
os_dup2.py
File metadata and controls
48 lines (36 loc) · 892 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
46
47
48
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-03-24 10:17:27
# @copyright by hoojo@2018
# @changelog Added python3 `os file -> dup2` example
import os
'''
概述
os.dup2() 方法用于将一个文件描述符 fd 复制到另一个 fd2。
Unix, Windows 上可用。
语法
dup2()方法语法格式如下:
os.dup2(fd, fd2);
参数
fd -- 要被复制的文件描述符
fd2 -- 复制的文件描述符
返回值
没有返回值
'''
# 打开文件
fd = os.open('/tmp/foo.txt', os.O_CREAT|os.O_RDWR)
# 写入文件
os.write(fd, 'this is new line')
# 复制文件
fd2 = 1000
os.dup2(fd, fd2)
# 移动文件指针
os.lseek(fd2, 0, 0)
print('读取文件内容:%s' % os.read(fd2, 100))
# 关闭文件
os.close(fd)
#os.closerange(fd, fd2)
print('操作完成')