Python | os.dup2() method Last Updated : 13 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 be 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 the 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 Python3 # Python3 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 descriptor # will correspond to the same # file to which original file # descriptor was referring # 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") 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 Comment More infoAdvertise with us Next Article Python | os.get_blocking() method I ihritik Follow Improve Article Tags : Python python-os-module Practice Tags : python Similar Reads OS Module in Python with Examples The 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.The *os* and *os.path* modules include many functions to interact with the fil 10 min read Process ParametersPython | os.ctermid() methodOS 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. os.ctermid() method in Python is used to get the filename corresponding to the co 1 min read Python | os.environ objectos.environ in Python is a mapping object that represents the userâs OS environmental variables. It returns a dictionary having the userâs environmental variable as key and their values as value.os.environ behaves like a Python dictionary, so all the common dictionary operations like get and set can 5 min read Python - os.chdir() methodos.chdir() method in Python is used to change the current working directory to the specified path. This function is part of the os module, which provides functionalities to interact with the operating system.Example of os.chdir()Pythonimport os # Check the current working directory print(os.getcwd() 2 min read Python | os.fchdir() methodOS 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. os.fchdir() method in Python is used to change the current working directory to t 3 min read Python | os.getcwd() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.getenv() methodOS module in Python provides functions for interacting with the operating system. OS comes under Python OS env standard utility modules. This module provides a portable way of using operating system-dependent functionality. os.getenv() method in Python OS env returns the value of the os environment 4 min read Python | os.get_exec_path() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.geteuid() and seteuid() methodAll functions in the OS Module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.geteuid() method in Python is used to get the current processâs effective user ID while os.seteuid 3 min read Python | os.getgrouplist() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.getgroups() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.getlogin() methodOS 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. os.getlogin() method in Python is used to get the name of the user logged in on t 1 min read Python | os.getpgid() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read Python | os.getpgrp() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.getpid() methodOS 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. os.getpid() method in Python is used to get the process ID of the current process 2 min read Python | os.getppid() methodOS 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. os.getppid() method in Python is used to get the parent process ID of the current 2 min read Python | os.getresuid() and os.setresuid() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 4 min read Python | os.getuid() and os.setuid() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read Python | os.setregid() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read Python | os.setreuid() methodAll functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.setreuid() method in Python is used to set the current processâs real and effective user IDs. In t 2 min read Python | os.setgroups() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.getsid() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.strerror() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read Python | os.supports_bytes_environ objectOS 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.os.supports_bytes_environ object in Python is used to check whether the native OS 1 min read Python | os.umask() methodOS 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. os.umask() method in Python is used to set the current numeric umask value and ge 2 min read Inheritance of File DescriptorsPython | os.set_inheritable() methodOS 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. os.set_inheritable() method in Python is used to set the value of inheritable fla 2 min read Python | os.get_terminal_size() methodOS 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. os.get_terminal_size() method in Python is used to query the size of a terminal. 1 min read Interface to the schedulerPython | os.sched_rr_get_interval() methodOS 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.OS module contains some methods which provides an interface to the scheduler and u 2 min read Python | os.sched_setaffinity() methodOS 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. os.sched_setaffinity() method in Python is used to set the CPU affinity mask of a 2 min read Python | os.sched_getaffinity() methodOS 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. os.sched_getaffinity() method in Python is used to get the set of CPUs on which t 2 min read Python | os.confstr() methodOS 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. os.confstr() method in Python is used to get string-valued system configuration v 4 min read System InformationPython | os.sysconf() methodOS 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. os.sysconf() method in Python is used to get integer-valued system configuration 4 min read Python | os.access() Methodos.access() method uses the real uid/gid to test for access to the path. Most operations use the effective uid/gid, therefore, this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to the path. In this article, we will learn about the access() func 3 min read Python | os.chflags() methodOS module in Python provides functions for interacting with the operating system. This comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.chflags() method in Python used to set the flags of path to the numeric flag 2 min read Python | os.chown() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 5 min read Python - os.chroot() methodos.chroot() method in Python is used to change the root directory of the current process to path. Syntax: os.chroot(path)Parameters: path: path to be set as root for the current process.Returns: does not return any value. Code #1: Python3 # Python program to explain os.chroot() method import os, sys 1 min read Files and DirectoriesPython | os.link() methodos.link() method in Python is used to create a hard link. This method creates a hard link pointing to the source named destination. In this article, we will see what is os link and the uses of os.link(). Note: This method is only available on Windows and Unix platforms. os.link() Method Syntax in Py 2 min read Python | os.listdir() methodThe os.listdir() method in Python is used to get the list of all files and directories in the specified directory. If we donât specify any directory, then a list of files and directories in the current working directory will be returned. os.listdir() Method Syntax in Python Syntax: os.listdir(path) 3 min read Python | os.mkdir() methodAll functions in the OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.mkdir() method in Python is used to create a directory in Python or create a directory with Python 4 min read Python | os.makedirs() methodAll functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system. In this article, we will see how to create directories recursively using the os module and also about 3 min read Python os.mkfifo() methodOS module in Python provides functions for interacting with the operating system. OS module comes under Pythonâs standard utility modules. os.mkfifo() method is used to create a FIFO (a named pipe) named path with the specified mode. FIFOs are named pipe which can be accessed like other regular file 3 min read Python | os.major() methodOS 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. os.major() method in Python is used to extract the device major number from the s 2 min read Python | os.minor() methodOS 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. os.minor() method in Python is used to extract the device minor number from the s 2 min read Python | os.makedev() methodOS 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. os.makedev() method in Python is used to compose a raw device number from the giv 3 min read Python | os.readlink() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.remove() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read Python | os.removedirs() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read Python | os.rename() methodOS 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. To rename a file or directory in Python you can use os.rename() function of OS mo 2 min read Python | os.renames() methodOS 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. os.renames() method is a recursive directory or file renaming function. It works 2 min read Python - os.replace() methodPrerequisite: OS module in Python. os.replace() method in Python is used to rename the file or directory. If destination is a directory, OSError will be raised. If the destination exists and is a file, it will be replaced without error if the action performing user has permission. This method may fa 2 min read Python | os.rmdir() methodos.rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory. All functions in the OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, 2 min read os.scandir() methodThe os.scandir() method in Python is used to iterate over the entries (files and directories) in a specified directory. It is more efficient than older methods like os.listdir() when you need information such as file type, size or metadata, because it avoids extra system calls. We use the following 3 min read Python | os.stat() methodOS comes under Python standard utility modules. This module provides a portable way of using operating system-dependent functionality. os.stat() method in Python performs stat() system calls on the specified path. This method is used to get the status of the specified path. os.stat() Method Syntax i 3 min read Python | os.statvfs() methodOS 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. os.statvfs() method in Python is used to get the information about the mounted fi 2 min read Python | os.sync() methodOS 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.os.sync() method in Python is used to force write of everything to disk. This meth 2 min read Python | os.truncate() methodOS 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. os.truncate() method in Python is used to truncate the file indicated by the spec 4 min read Python | os.unlink() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 3 min read os.walk() in PythonHow to traverse file system in Python ? Suppose we have given below file structure in our system and we want to traverse all it's branches completely from top to bottom ? How does os.walk() work in python ?OS.walk() generate the file names in a directory tree by walking the tree either top-down or b 2 min read Python | os.abort() methodOS 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. os.abort() method in Python is used to generate a SIGABRT signal to the current p 2 min read Python | os._exit() methodOS 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. os._exit() method in Python is used to exit the process with specified status wit 2 min read Python | os.fork() methodOS 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.All functions in os module raise OSError in the case of invalid or inaccessible fi 2 min read Python | os.kill() methodos.kill() method in Python is used to send a specified signal to the process with a specified process ID. Constants for the specific signals available on the host platform are defined in the Signal Module. os.kill() Method Syntax in PythonSyntax: os.kill(pid, sig) Parameters: pid: An integer value r 2 min read Python | os.nice() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 4 min read Python | os.system() methodThe 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.os.system() method executes the command (a string) in a subshell. This method 3 min read Python | os.times() methodOS 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. os.times() method in Python is used to get the current global process times. Synt 1 min read Python | os.wait() methodOS 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. os.wait() method in Python is used by a process to wait for completion of a child 2 min read Python | os.open() methodOS 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. os.open() method in Python opens a specified file path. This method returns a fil 3 min read Process ManagementPython | os.device_encoding() methodOS 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. os.device_encoding() method in Python is used to get the encoding of the device a 2 min read Python | os.dup() methodOS 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 inpu 3 min read Python | os.dup2() methodOS 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 inpu 3 min read Python | os.fchmod() methodOS 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.In Unix-like systems, Modes are file system permissions given to user, group and o 3 min read Python | os.fchown() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 4 min read Python | os.fdatasync() methodOS 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. os.fdatasync() method in Python is used to force write of the file associated wit 2 min read Python | os.fstat() methodOS 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. os.fstat() method in Python is used to get the status of a file descriptor. A fil 3 min read Python | os.fstatvfs() methodOS 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. os.fstatvfs() method in Python is used to get information about the file system c 3 min read File Descriptor OperationsPython | os.get_blocking() methodOS 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. os.get_blocking() method in Python is used to get the blocking mode information o 2 min read Python | os.isatty() methodOS 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. os.isatty() method in Python is used to check whether the specified file descript 2 min read Python | os.openpty() methodOS 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. os.openpty() method in Python is used to open a new pseudo-terminal pair. This me 1 min read Python | os.pipe() methodOS 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. All functions in os module raise OSError in the case of invalid or inaccessible f 2 min read Python | os.pipe2() methodOS 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 pipe is a method to pass information from one process to another process. It of 2 min read Python | os.pread() methodOS 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. os.pread() method in Python is used to read at most n bytes from the file associa 3 min read Python | os.write() methodOS 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. os.write() method in Python is used to write a bytestring to the given file descr 2 min read Python | os.pwrite() methodOS 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. os.pwrite() method in Python is used to write the specified bytestring to the fil 3 min read Python | os.read() methodOS 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. os.read() method in Python is used to read at most n bytes from the file associat 2 min read Python | os.sendfile() methodOS 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. os.sendfile() method in Python is used to copy specified number of bytes from spe 2 min read Python | os.set_blocking() methodOS 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. os.set_blocking() method in Python is used to set the blocking mode of the specif 2 min read Like