Python3 Os Dup
# Python3.x Python3 os.dup() Method
[ Python3 OS File/Directory Methods](#)
* * *
### Overview
The os.dup() method is used to duplicate the file descriptor fd.
### Syntax
The syntax for the **dup()** method is as follows:
os.dup(fd);
### Parameters
* **fd** -- file descriptor
### Return Value
Returns the duplicated file descriptor.
### Example
The following example demonstrates the use of the dup() method:
## Example (Python 3.0+)
#!/usr/bin/python3 import os, sys# Open a file fd = os.open("foo.txt", os.O_RDWR|os.O_CREAT)# Duplicate the file descriptor d_fd = os.dup(fd)# Write to the file using the duplicated file descriptor os.write(d_fd, "This is test".encode())# Close the files os.closerange(fd, d_fd)print("All files closed successfully!!")
The output of executing the above program is:
All files closed successfully!!
[ Python3 OS File/Directory Methods](#)
YouTip