理解Python中fileno() ||os.open() || bytearray||flush

  • file object

    An object exposing a file-oriented API to an underlying resource.

    File objects are also called file-like objects or streams

    There are actually three categories of file objects:

    • raw binary files
    • buffered bianry files
    • text files

    Their interfaces are defined in the io module.

    The canonical way to create a file object is by using the open() function.

  • bytearray

    a mutable counterpart to bytes objects.

  • file.fileno()

    Return the integer file descriptor.

    File descriptor is a low-level concept, it’s an integer that represents an open file.

    Each open file is given a unique file descriptor.

    Refered on wikipedia, a file descriptor(FD) is an abstract indicator(handle) used to access a file or other input/output resource, such as a pipe or network socket.

    In the traditional implementation of Unix, file descriptors index into a per-process file descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the file table.

    File table records the mode with which the file has been opened: for reading, writing, appending, and possibly other modes.

    It also indexes into a third table called the inode table that describes the actual underlying files.

    To perform input or output, the process passes the file descriptor to the kernel through a system call, and the kernel will access the file on behalf of the process. The process does not have direct access to the file or inode tables.

    On Linux, the set of file descriptors open in a process can be accessed under the path /proc/PID/fd/, where PID is the process identifier.

  • os.flush()

    Flush the write buffers of the stream if applicable.

    This does nothing for read-only and non-blocking streams.

  • References

  1. Glossary
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章