Python3 os.dup() Method

Python3 OS File/Directory Methods


Overview

The os.dup() method is used to duplicate a 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:

Example (Python 3.0+)

#!/usr/bin/python3 import os, sys # Open file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Duplicate file descriptor d_fd = os.dup( fd ) # Write to file using the duplicated file descriptor os.write(d_fd, "This is test".encode()) # Close file os.closerange( fd, d_fd) print ("All files closed successfully!!")

The output of the above program is:

Successfully closed all files!!

Python3 OS File/Directory Methods

Other Extensions