Latest web development tutorials

Python3 os.dup() 方法

Python3 OS 文件/目錄方法 Python3 OS文件/目錄方法


概述

os.dup() 方法用於復製文件描述符fd。

語法

dup()方法語法格式如下:

os.dup(fd);

參數

  • fd --文件描述符

返回值

返回複製的文件描述符。

實例

以下實例演示了dup() 方法的使用:

#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 复制文件描述符
d_fd = os.dup( fd )

# 使用复制的文件描述符写入文件
os.write(d_fd, "This is test")

# 关闭文件
os.closerange( fd, d_fd)

print ("关闭所有文件成功!!")

執行以上程序輸出結果為:

关闭所有文件成功!!

Python3 OS 文件/目錄方法 Python3 OS文件/目錄方法