Python方法dup()返回文件描述符fd的副本,可用于代替原始描述符。

os.dup(fd) - 语法

os.dup(fd);
  • fd  -  这是原始文件描述符。

os.dup(fd) - 返回值

此方法返回文件描述符的副本。

os.dup(fd) - 示例

以下示例显示dup()方法-的用法

#!/usr/bin/python

import os, sys

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

# Get one duplicate file descriptor
d_fd=os.dup( fd )

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

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

print "Closed all the files successfully!!"

当无涯教程运行上面的程序时,它产生以下输出-

Closed all the files successfully!!

参考链接

https://www.learnfk.com/python/os-dup.html