Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.dup2() Method

Python3 os.dup2() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.dup2() method duplicates one file descriptor fd onto another fd2.

Available on Unix and Windows.

Syntax

The **dup2()** method syntax is as follows:



os.dup2(fd, fd2);

Parameters

Return Value

No value is returned.

Example

The following example demonstrates the use of the dup2() method:

Example

import

os

Open a file

f

=

open

(

'

txt

'

,

'

a

'

)

Pass the file represented by this file descriptor to the file pointed to by descriptor 1 (that is, stdout)

os

.

dup2

(

f

.

fileno

(

)

,

1

)

Closing a File

f

.

close

(

)

print writes to the standard output stream, which is file descriptor 1

print

(

'

runoob

'

)

print

(

'

google

'

)

Running the above program produces a txt file whose content is:



runoob

google

Python3 OS File/Directory Methods Python3 OS File/Directory Methods