Python3 os.dup()方法

dup()方法返回檔描述符fd,其可以代替原來描述使用。

語法

以下是 dup() 方法的語法:
os.dup(fd)

參數

  • fd -- 這是原始檔描述符

返回值

這個方法返回的檔描述符的副本

示例

下麵的示例顯示 dup() 方法的用法:
#!/usr/bin/python3

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

line="this is test"
# string needs to be converted byte object
b=str.encode(line)

os.write(d_fd, b)

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

print "Closed all the files successfully!!"
當我們運行上面的程式,它會產生以下結果:
Closed all the files successfully!!

上一篇: Python3檔方法 下一篇: Python3 os檔目錄的方法