28.10.2021 Views

Python Tutorial ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Parameters

• fd -- This is the original file descriptor.

Return Value

This method returns a duplicate of file descriptor.

Example

The following example shows the usage of dup() method.

#!/usr/bin/python

import os, sys

# Open a file

fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Get one duplicate file descriptor

d_fd = os.dup( fd )

# Write one string using duplicate fd

os.write(d_fd, "This is test")

# Close a single opened file

os.closerange( fd, d_fd)

print "Closed all the files successfully!!"

Let us compile and run the above program, this will produce the following result:

Closed all the files successfully!!

os.dup2(fd, fd2)

Description

The method dup2() duplicates file descriptor fd to fd2, closing the latter first if necessary.

Note: New file description would be assigned only when it is available. In the following example given below,

1000 would be assigned as a duplicate fd in case when 1000 is available.

Syntax

Following is the syntax for dup2() method:

os.dup2(fd, fd2);

Parameters

• fd -- This is File descriptor to be duplicated.

• fd2 -- This is Duplicate file descriptor.

Return Value

This method returns a duplicate of file descriptor.

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!