Easy Tutorial
❮ Python Calendar Python Pip ❯

Python3 os.dup() Method

Python3 OS File/Directory Methods


Overview

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

Syntax

dup() 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 a file
fd = os.open("foo.txt", os.O_RDWR|os.O_CREAT)

# Duplicate the file descriptor
d_fd = os.dup(fd)

# Write to the file using the duplicated file descriptor
os.write(d_fd, "This is test".encode())

# Close the files
os.closerange(fd, d_fd)

print("Successfully closed all files!!")

Executing the above program outputs:

Successfully closed all files!!

Python3 OS File/Directory Methods

❮ Python Calendar Python Pip ❯