Skip to content

Latest commit

 

History

History
82 lines (50 loc) · 2.12 KB

fileio.md

File metadata and controls

82 lines (50 loc) · 2.12 KB

fileioimage title

contents

related file

  • cpython/Modules/_io/fileio.c

memory layout

memory layout

overview

a+

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

1_txt_a+

close

After call the close method, the fd becomes -1, and one more key __IOBase_closed inserted to dict field

>>> f.close()

1_txt_close

rb

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")

2_txt_rb

fd

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>"

fd_3