os.fdopen(fd, [, mode[, bufsize]]);
fd -- 這是要返回的一個檔案物件的檔案描述符。
mode -- 這個可選引數是表示檔案是如何被開啟的字串。大部分的模式常用的值是「r」表示讀,"w"表示寫入(截斷檔案,如果它已經存在),以及"a"表示追加。
bufsize -- 此可選引數指定檔案的所需的緩衝區大小: 0 表示無緩衝, 1 表示行緩衝, 任何其他正值表示要真正使用(約)大小的緩衝。
#!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Now get a file object for the above file. fo = os.fdopen(fd, "w+") # Tell the current position print ("Current I/O pointer position :%d" % fo.tell()) # Write one string fo.write( "Python is a great language.\nYeah its great!!\n"); # Now read this file from the beginning. os.lseek(fd, 0, 0) str = os.read(fd, 100) print ("Read String is : ", str) # Tell the current position print "Current I/O pointer position :%d" % fo.tell() # Close opened file fo.close() print ("Closed the file successfully!!")
Current I/O pointer position :0 Read String is : This is testPython is a great language. Yeah its great!! Current I/O pointer position :45 Closed the file successfully!!