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!!")
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!!"
Python3 os 模块 Closerange 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Write one string line = "this is test" # string needs to be converted byte object b = str.encode(line) os.write(fd, b) # Close a single opened file os.closerange( fd, fd) print ("Closed all the files successfully!!")
Python3 os 模块 Close 方法 #!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Write one string line = "this is test" # string needs to be converted byte object b = str.encode(line) os.write(fd, b) # Close opened file os.close( fd ) print ("Closed the file successfully!!")
Python3 文件File Seek 方法 #!/usr/bin/python3 # Open a file fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.readlines() print ("Read Line: %s" % (line)) # Again set the pointer to the beginning fo.seek(0, 0) line = fo.readline() print ("Read Line: %s" % (line)) # Close opened file fo.close()
Python3 文件File Read 方法 #!/usr/bin/python3 # Open a file fo = open("foo.txt", "r+") print ("Name of the file: ", fo.name) line = fo.read(10) print ("Read Line: %s" % (line)) # Close opened file fo.close()