os.dup() Overview
The os.dup() method is used to copy the file descriptor fd.
os.dup() Syntax
The dup() method syntax format is as follows:
os.dup(fd);
os.dup() Parameter
- fd – file descriptor
os.dup() Return Value
Returns the copied file descriptor.
os.dup() Demo
The following example demonstrates the use of the dup() method:
#!/usr/bin/python3
import os, sys
# Open file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
#Copy file descriptors
d_fd = os.dup( fd )
# Writing to a file using a copied file descriptor
os.write(d_fd, "This is test".encode())
# Close file
os.closerange( fd, d_fd)
print ("Close all files successfully!!!")
The output of executing the above program is:
Close all files successfully!!!