Python3 os.dup2() Method
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
-
fd -- The file descriptor to be duplicated
-
fd2 -- The target file descriptor
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
(
'
runoob
'
)
(
'
'
)
Running the above program produces a txt file whose content is:
runoob
google