Python3 os.dup2() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

os.dup2() method is used to duplicate a file descriptor fd to another fd2.

Available on Unix, Windows.

Syntax

dup2()The method syntax is as follows:

os.dup2(fd, fd2);

Parameters

  • fd-- fd to be duplicated

  • fd2-- the duplicated file descriptor

Return Value

No return value.

Example

The following example demonstrates the usage 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 (i.e., stdout) os.dup2(f.fileno(), 1) # Close the file f.close() # print outputs to the standard output stream, which is file descriptor 1 print('example') print('google')

After executing the above program, the output result is that a txt file is generated, and the content is:

example
google

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions