Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Python | os.dup() & 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.dup() method in Python is used to duplicate the given file descriptor. The duplicated file
descriptor is non-inheritable, But on Windows platform, file descriptor associated with
standard stream (standard input: 0, standard output: 1, standard error: 2) which can be
inherited by child processes.

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.dup(fd)
Parameter:
fd: A file descriptor, which is to be duplicated.
Return Type: This method returns the duplicated file descriptor, which is an integer value.

You might also like