current position:Home>Python learning notes - the fourth bullet IO operation

Python learning notes - the fourth bullet IO operation

2022-02-01 07:04:44 goonwalk

「 This is my participation 11 The fourth of the yuegengwen challenge 15 God , Check out the activity details :2021 One last more challenge 」.

IO In a computer, it means input/output, That's input and output . Because the program and runtime data reside in memory , from CPU This super fast computing core to execute , Where it comes to data exchange , Usually disk 、 Network, etc , Need IO Interface .

1. File read and write

1.1 Reading and writing files is the most common IO operation .Python Built in functions for reading and writing files , Usage and C Is compatible .

1.2 Before reading and writing the document , You need to review the knowledge related to the operating system first : The function of reading and writing files on disk is provided by the operating system , However, the operating system does not allow ordinary programs to directly operate the disk .

1.3 To read and write a file is to ask the operating system to open a file object ( Usually called a file descriptor fds), then , Read data from this file object through the interface provided by the operating system ( Reading documents ), Or write the data to the file object ( Writing documents ).

Reading documents

To open a file object in read file mode , Use Python Built in open() function , Pass in the file name and operation mode identifier as function parameters :

image.png

2. python Reading and writing

image.png

notes :

"r" Indicates read-only read, That is, read the file . If not set , This mode is adopted by default .

"w" Means write write, And it is written to the file by overwriting .

"x" Representation creation create A new file , Open and write content .

"a" Means to open and append... At the end of the file append Content ( If the file exists ).

"b" For binary binary Mode writing .

"t" Represents a text pattern text.

"+" Indicates read and write .

The system default value is "rt"

If the file opens successfully , Next , call read() Method can read all the contents of the file at once .

The last step is to call close() Method to close the file , Note that the file must be closed after use , otherwise , I don't know where the file is offset , It will be easy to misoperate the contents of the file .

copyright notice
author[goonwalk],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010704429457.html

Random recommended