二十五岁时我们都一样愚蠢、多愁善感,喜欢故弄玄虚,可如果不那样的话,五十岁时也就不会如此明智。
标题:Python3 os 模块 Dup2 方法
Python3 os 模块 Dup2 方法
#!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Write one string using duplicate fd line = "this is test" # string needs to be converted byte object b = str.encode(line) os.write(fd, b) # Now duplicate this file descriptor as 1000 fd2 = 1000 os.dup2(fd, fd2); # Now read this file from the beginning using fd2. os.lseek(fd2, 0, 0) line = os.read(fd2, 100) str = line.decode() print ("Read String is : ", str) # Close opened file os.closerange( fd,fd2 ) print ("Closed the file successfully!!")