YouTip LogoYouTip

Python3 Os Dup

# Python3.x Python3 os.dup() Method [![Image 3: Python3 OS File/Directory Methods](#) 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!! [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Python3 Os DupPython3 Os Close β†’