Python3 os 模块 Dup 方法


Python3 os 模块 Dup 方法

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Get one duplicate file descriptor
d_fd = os.dup( fd )

# Write one string using duplicate fd
line = "this is test"

# string needs to be converted byte object
b = str.encode(line)
os.write(d_fd, b)

# Close a single opened file
os.closerange( fd, d_fd)

print ("Closed all the files successfully!!")