Python | os.dup2 () method

Python Methods and Functions

File descriptor is a small integer value that corresponds to a file or other I / O resource, such as a pipe or network socket. A file descriptor is an abstract resource indicator and acts as a descriptor for performing various lower level I / O operations such as reading, writing, sending, etc.

For example: standard input is usually a file descriptor with a value of 0, standard output is usually a file descriptor with a value of 1, and standard error is usually a file descriptor with a value of 2.
Other files opened by the current process will get values ​​3, 4, 5, etc. D.

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

"Inherited file descriptor" means that if the parent process has file descriptor 4 used for a specific file, and the parent process creates a child process, then the child process will also use file descriptor 4 for the 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 ie duplicate file descriptor.

Code: using the os.dup2 () method to duplicate a file descriptor

Exit :

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




Get Solution for free from DataCamp guru

# Python program for os.dup2 () method explanations

 
# import of the os module

import os

 
# File path

path = " / home / ihritik / Desktop / file.txt "

  

 
# open the file and read
# the file descriptor associated
# using the os.open () method

fd = os. open (path, os.O_WRONLY)

 
# Print value
# file descriptor

print ( " Original file descriptor: " , fd)

 
# Duplicate file descriptor
# using the os.dup2 () method

dup_fd = 7

os.dup2 (fd, dup_fd)

  
# Duplicate file descriptor
# will match the same
# the file to which the source file belongs
# the descriptor summarized

 
# Print value
# duplicate file descriptor

print ( "Duplicated file descriptor:" , dup_fd)

 

 
# Get a list of all
# File descriptors used
# by the current process
# (The code below works on UNIX systems)

pid = os.getpid ()

os.system ( "ls -l / proc /% s / fd" % pid)

 
# Close descriptors files
os.close (fd)
os.close (dup_fd)

 

print ( "File descriptor duplicated successfully" )