Python os.dup() Method

Python OS File/Directory Methods


Overview

The os.dup() method is used to duplicate the file descriptor fd.

Syntax

dup()The method syntax is as follows:

os.dup(fd);

Parameters

  • fd-- file descriptor

Return Value

Returns the duplicated file descriptor.

Example

The following example demonstrates the use of the dup() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

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 "关闭所有文件成功!!"

The output of the above program is:

All files closed successfully!!

Python OS File/Directory Methods

Other Extensions