YouTip LogoYouTip

Os Dup2

# Python2.x Python os.dup2() Method [![Image 3: Python File Methods](#) Python OS File/Directory Methods](#) * * * ### Overview The os.dup2() method is used to duplicate a file descriptor fd to another fd2. Available on Unix and Windows. ### Syntax The syntax for the **dup2()** method is as follows: os.dup2(fd, fd2); ### Parameters * **fd** -- The file descriptor to be duplicated. * **fd2** -- The file descriptor to duplicate to. ### Return Value This method does not return a value. ### Example The following example demonstrates the use of the dup2() method: #!/usr/bin/python# -*- coding: UTF-8 -*-import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )# Write a string os.write(fd, "This is test")# File descriptor is 1000 fd2 = 1000 os.dup2(fd, fd2);# Insert data on the new file descriptor os.lseek(fd2, 0, 0) str = os.read(fd2, 100)print "The read string is : ", str # Close the file os.close( fd )print "Closed the file successfully!!" The output of executing the above program is: The read string is : This is test Closed the file successfully!! [![Image 4: Python File Methods](#) Python OS File/Directory Methods](#)
← Os FchmodOs Chroot β†’