Io is the input/output in the computer, that is, input and output. Because the program and runtime data resides in memory and is executed by the CPU, an ultra-fast compute core, where the data is exchanged, usually a disk, a network, and so on, requires an IO interface.
As long as the data exchange, network transmission and other behavior will generate IO operations.
Synchronization io:cpu Wait, that is, the program pauses the execution of subsequent code, such as 100M of data in 10 seconds after writing to the disk, and then down execution.
Asynchronous io:cpu do not wait, just tell the disk, "you old slowly write, do not worry, I went on to do something else," then, the subsequent code can be executed immediately.
The difference between synchronous and asynchronous is whether to wait for the results of IO execution.
File Read and write:
Writing files is the most common IO operation. Python has built-in functions for reading and writing files, and the usage is compatible with C.
Before reading and writing files, we must first understand that the ability to read and write files on disk is provided by the operating system, the modern operating system does not allow ordinary programs to operate the disk directly, so, read-write files are requesting the operating system to open a file object (often referred to as a file descriptor),
The data is then read from the file object by the interface provided by the operating system (read file), or the data is written to the file object (write file).
Read the file
To open a file object in read-file mode, use Python's built-in open() functions to pass in file names and identifiers:
If the file is opened successfully, then the calling read() method can read the entire contents of the file at once, and Python reads the contents into memory, represented by an str object:
The final step is to call the close() method to close the file. The file must be closed after it is used because the file object consumes the resources of the operating system, and the number of files that the operating system can open at the same time is limited:
Since the file is read and written, it is possible to generate it, and IOError in the event of an error, f.close() it is not called. Therefore, in order to ensure that the file is closed correctly, whether or not it is an error, we can use it try ... finally to implement:
Try: f = open (' C://1.txt ', ' R ') print f.read () finally: if f: F.close ()
Python introduces a with statement to help us invoke the close() method automatically:
With open (' C://1.txt ', ' R ') as F: print F.read ()
This is the same as before try ... finally , but the code is more concise and does not have to call the f.close() method.
The call read() reads the entire contents of the file one time, and if the file has 10G, the memory explodes, so, to be safe, you can call the read(size) method repeatedly, reading the contents of a size byte at most. In addition, the call readline() can read one line at a time, and the call readlines() reads all the contents once and returns by row list . Therefore, you need to decide how to call as needed.
If the file is small, one-time read() reading is the most convenient, if the file size can not be determined, repeated calls to read(size) compare insurance; If the configuration file, the call is readlines() most convenient:
For line in F.readlines (): print (Line.strip ()) # Erase the end of ' \ n '
Character encoding
To read a non-ASCII encoded text file, it must be opened in binary mode and decoded. Like GBK encoded files.
>>> f = open ('/users/michael/gbk.txt ', ' RB ') >>> U = F.read (). Decode (' GBK ') >>> uu ' \u6d4b\ U8bd5 ' >>> print u test
Write a file
Writing and reading files are the same, the only difference being that when a function is called, an identifier is passed in or a write open() - ‘w‘ ‘wb‘ in file or a binary file is written:
With open ('/users/michael/test.txt ', ' W ') as F: f.write (' Hello, world! ')
Common read-write mode for files
W opens in write mode,
W file if present, first to clear, then (RE) create
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read/write mode (see W)
A + opens in read/write mode (see a)
RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)
File open Mode w+ r+ A + difference and discrimination
w+ Open the file and read and write:
1. File exists, then empty (also write empty);
2. If the file does not exist, create a file;
3. The file stream is positioned to the start location, so read () will be empty.
r+ Open the file and read and write:
1. File exists, open file, file pointer to the beginning of the file location;
2. The file does not exist, the error file does not exist.
A + opens the file and reads and writes:
1. File exists, open the file, the file pointer locates to the beginning of the file, but does not empty;
2. File does not exist, create file;
3. When read is open, at the beginning of the file,
4. When writing, it is added to the end of the article, and the pointer is at the end of the add, so reading again is garbled.
Other than that:
1. W Open the file to write, also will empty the file, if using read (), the error; A open file add, the data stream is added to the end of the file, instead of W mode is emptied, added to the end of the file.
2. B can be attached to the above letter, the formation of RB, rb+, WB and other modes, for binary files, such as EXE, ELF, JPEG files, file operation; on Unix-type systems, the text format is the same as binary processing,
However, non-UNIX-type systems have different line-wrapping formats, so you need to use the plus B mode to specify whether binary is binary.
Python IO Programming