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

Python os.dup() method

Python os.dup() method

Python File methods Python OS file/directory methods


Overview

The os.dup() method duplicates the file descriptor fd.

Syntax

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



os.dup(fd);

Parameters

Return Value

Returns the duplicated file descriptor.

Example

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



#!/usr/bin/python

# -*- coding: UTF-8 -*-



import os, sys



# Open file

fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )



# Copy file descriptor

d_fd = os.dup( fd )



# Use copied file descriptor to write to file

os.write(d_fd, "This is test")



# Close file

os.closerange( fd, d_fd)



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

The output of the above program is:

关闭所有文件成功!!

Python File methods Python OS file/directory methods