Python3 os.dup2() 方法

Python 3 教程 · 2019-02-07 09:26:29

概述

os.dup2() 方法用于将一个文件描述符 fd 复制到另一个 fd2。

Unix, Windows 上可用。

语法

dup2()方法语法格式如下:

os.dup2(fd, fd2);

参数

  • fd -- 要被复制的文件描述符

  • fd2 -- 复制的文件描述符

返回值

没有返回值。

实例

以下实例演示了 dup2() 方法的使用:

实例

import os # 打开一个文件 f=open('txt','a') # 将这个文件描述符代表的文件,传递给 1 描述符指向的文件(也就是 stdout) os.dup2(f.fileno(), 1) # 关闭文件 f.close() # print 输出到标准输出流,就是文件描述符1 print('codercto') print('google')

执行以上程序输出结果为, 生成一个txt文件,内容是::

codercto
google

点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html

查看所有标签

The Information

The Information

James Gleick / Vintage / 2012-3-6 / USD 16.95

James Gleick, the author of the best sellers Chaos and Genius, now brings us a work just as astonishing and masterly: a revelatory chronicle and meditation that shows how information has become th......一起来看看 《The Information》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具