Python os.dup2() Method
Example
Duplicate a file descriptor to a given value:
# import os Library
import os
# open file
fd = os.open( "file.txt",
os.O_RDWR|os.O_CREAT )
# Print value of file descriptor
print("Original file descriptor:", fd)
# Duplicate file descriptor
dup_fd = 9
os.dup2(fd, dup_fd)
# Print value of duplicate
file descriptor
print("Duplicated file descriptor:", dup_fd)
#
Close file descriptors
os.close(fd)
os.close(dup_fd)
Run Code »
Definition and Usage
The os.dup2()
duplicates a file descriptor (fd) to
a given value (fd2).
Syntax
os.dup2(fd, fd2, inheritable)
Parameter Values
Parameter | Description | |
---|---|---|
fd | Required. The file descriptor to be duplicated | |
fd2 | Required. The duplicate value of file descriptor | |
inheritable | Optional. True means that the duplicated file descriptor is inheritable by child processes. False makes it non-inheritable. Default value is True |
Technical Details
Return Value: | A int value, representing fd2.
Returns -1 on failure.
|
---|---|
Python Version: | 2.6 |
Change Log: | 3.4: Added inheritable parameter 3.7: Now returns fd2 on success. Previously, None was returned |