Python | os.dup2() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

A file descriptor is small integer value that corresponds to a file or other input/output resource, such as a pipe or network socket. A File descriptor is an abstract indicator of a resource and act as handle to perform various lower level I/O operations like read, write, send etc.

For Example: Standard input is usually file descriptor with value 0, standard output is usually file descriptor with value 1 and standard error is usually file descriptor with value 2.
Further files opened by the current process will get the value 3, 4, 5 an so on.



os.dup2() method in Python is used to duplicate a file descriptor fd to a given value fd2. The file descriptor will duplicated to fd2 only if fd2 is available and duplicated file descriptor is inheritable by default.

Inheritable file descriptor means if the parent process has a file descriptor 4 in use for a particular file and parent creates a child process then the child process will also have file descriptor 4 in use for that same file.

Syntax: os.dup2(fd, fd2, inheritable = True)

Parameter:
fd: A file descriptor, which is to be duplicated.
fd2: This is duplicate value of file descriptor.
inheritable (optional): A Boolean value, True or False. The default value of this parameter is True, which means duplicated file descriptor is inheritable by child processes. To make it non-inheritable set it to False.

Return Type: This method returns the second parameter fd2 i.e duplicate file descriptor.

Code: Use of os.dup2() method to duplicate a file descriptor

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.dup2() method 
    
# importing os module 
import os
  
# File path 
path = "/home/ihritik/Desktop/file.txt"
  
  
# open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_WRONLY)
  
# Print the value of
# file descriptor
print("Original file descriptor:", fd)
  
# Duplicate the file descriptor
# using os.dup2() method
dup_fd = 7
os.dup2(fd, dup_fd)
  
# The duplicate file desciptor
# will correspond to the same 
# file to which original file
# descriptor was refering 
  
# Print the value of
# duplicate file descriptor
print("Duplicated file descriptor:", dup_fd)
  
  
# Get the list of all 
# file Descriptors Used
# by the current Process
# (Below code works on UNIX systems)
pid = os.getpid()
os.system("ls -l/proc/%s/fd" %pid)
  
# Close file descriptors
os.close(fd)
os.close(dup_fd)
  
print("File descriptor duplicated successfully")

chevron_right


Output:

Original file descriptor: 3
Duplicated file descriptor: 7
total 0
lrwx------ 1 ihritik ihritik 64 Jun 14 06:45 0 -> /dev/pts/0
lrwx------ 1 ihritik ihritik 64 Jun 14 06:45 1 -> /dev/pts/0
lrwx------ 1 ihritik ihritik 64 Jun 14 06:45 2 -> /dev/pts/0
l-wx------ 1 ihritik ihritik 64 Jun 14 06:45 3 -> /home/ihritik/Desktop/file.txt
l-wx------ 1 ihritik ihritik 64 Jun 14 06:45 7 -> /home/ihritik/Desktop/file.txt
File descriptor duplicated successfully


My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.