Python os.dup() Method
Example
Duplicate a file descriptor:
#Import os Library
import os
# open a file and create a file descriptor
fd = os.open( "test.txt", os.O_RDWR|os.O_CREAT )
# Duplicate File descriptor
duplicateFd = os.dup(fd)
#print both descriptors
print ("Orignal file descriptor:", fd)
print ("Duplicate file descriptor:", duplicateFd)
#close file
os.close(fd)
os.close(duplicateFd)
Run Code »
Definition and Usage
The os.dup()
method duplicates the given file descriptor.
The new file descriptor is non-inheritable.
Note: On Windows, when duplicating a standard stream, the new file descriptor is inheritable.
Note: Available on both Windows and Unix platforms.
Syntax
os.dup(fd)
Parameter Values
Parameter | Description |
---|---|
fd | Required. The file descriptor to be duplicated |
Technical Details
Return Value: | An int value, representing the duplicated file descriptor |
---|---|
Python Version: | 2.6 |
Change Log: | 3.4: The new file descriptor is now non-inheritable |