Latest web development tutorials

Python os.dup () method

Python File (File) method Python OS file / directory methods


Outline

os.dup () method is used to copy the file descriptor fd.

grammar

dup () method syntax is as follows:

os.dup(fd);

parameter

  • fd - file descriptor

return value

Copy the file descriptor returned.

Examples

The following example demonstrates the use dup () method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 复制文件描述符
d_fd = os.dup( fd )

# 使用复制的文件描述符写入文件
os.write(d_fd, "This is test")

# 关闭文件
os.closerange( fd, d_fd)

print "关闭所有文件成功!!"

The above program output is:

关闭所有文件成功!!

Python File (File) method Python OS file / directory methods