- cpython/Modules/_io/fileio.c
As python document said, the FileIO object represents an OS-level file containing bytes data
>>> import io
>>> f = io.FileIO("./1.txt", "a+")
>>> f.write(b"hello")
5
The fd field represent the low level file descriptor, the created flag is 0, readable, writable, appending, seekable, closefd are all 1.
blksize represents the os-level buffer size in bytes for the current fd
dict field stores the user-specific filename
For those who need the detail of python dict object, please refer to my previous article dict
After call the close method, the fd becomes -1, and one more key __IOBase_closed inserted to dict field
>>> f.close()
Now, let's open a file in read-only mode
the fd and dict object are all reused, and writable, appending, seekable field now becomes 0/-1
>>> f = io.FileIO("../../Desktop/2.txt", "rb")
let's pass an integer to parameter name
>>> f = open("../../Desktop/2.txt", "rb")
>>> f.fileno()
3
>>> f2 = io.FileIO(3, "r")
"<_io.FileIO name=3 mode='rb' closefd=True>"