Python-IO 流

What is an IP stream?

IO middle finger in the computer Input / Output, which is input and output. Popular, due to the run-time program and data reside in memory, this is performed by the CPU core ultra-fast computing, where it comes to data exchange, usually a disk, network, etc., we need IO interface.

For example, you open your browser, visit Sina home page, the browser will need to get this program over the network Sina page IO. First, the browser will send data to the server Sina, tell it to HTML, this action is out of the home I want to send data, called Output, followed by Sina web server to send over, this action is to receive data from the outside, called Input. Therefore, usually, the operation procedures are completed IO Input and Output have two data streams. Of course, there is only one case, for example, read files from disk into memory, only Input operation, in turn, to write data to disk file, just a Output operation.

IO programming, Stream (stream) is a very important concept, can be thought of as a stream of water pipes, water pipes in the water is the data, but only one-way flow. Input Stream data flow is from the outside into the memory (disk, network), Output Stream data flow is from the outside to the memory. For web browsing, the server between the browser and Sina need to build at least two pipes, it can both send data, but also to collect data.

Since the CPU and memory much faster than the speed peripherals, so the IO programming, there is a serious problem that does not match the speed. Take, for example, such as 100M of data should be written to disk, CPU 100M output data need only 0.01 seconds, but this 100M disk to receive data may take 10 seconds, how to do it? There are two ways:

The first CPU is waiting, the program is suspended subsequent code, and other data written to disk 100M in 10 seconds, and then down then performed, this mode is called the IO synchronization;

Another approach is to not wait for CPU, disk just told, "Your old write slowly, do not worry, I went on to do other things," and then, subsequent code can then be executed immediately, this mode is called asynchronous IO.

The difference between synchronous and asynchronous IO is whether to wait for the results of execution. Like you go to McDonald's meal, you say "to a hamburger," the waiter told you, I'm sorry, Hamburg to do now, to wait five minutes, then you stand in front of the cashier waited five minutes to get a burger to go shopping this is synchronous IO.

You say "to a hamburger," the waiter told you, Hamburg need to wait five minutes, you can go shopping, and so well, we'll let you know so that you can do otherwise (the mall) at once, which asynchronous IO.

Obviously, the use of asynchronous IO to write programs is much higher than the synchronous IO performance, but the drawback is that asynchronous IO programming model complexity. Think about it, you have to know when to let you know, "Hamburg well," and notice your approach is also different. If the waiter ran to find you, this is the correction mode, if the waiter send text messages to inform you that you have to constantly check the phone, which is polling mode. In short, complexity is much higher than synchronous asynchronous IO IO.

IO capability operation are provided by the operating system, programming language, each operating system will put a lower C interface to encapsulate easy to use, Python is no exception. As we will discuss in detail the Python programming interface IO.

OPEN function

First with Python built-in open () function to open a file, create a file object associated method can invoke it to read and write.

grammar:

file object = open(file_name 【, access_mode][, buffering])

file_name: file_name variable is a string value containing the name of the file you want to access.
access_mode: access_mode decided to open the file mode: Read, Write, and additions. See a complete list of all possible values as follows. This parameter is not mandatory, the default file is read-only access mode ®.
buffering: If the value of buffering is set to 0, there will be storage. If the value of buffering take 1, the line will register to access the file. If the value is an integer greater than 1 of buffering, indicating that this is the buffer size of the storage area. If negative, the buffer size for the system default parking zone.

File object properties

After a file is opened, you have a file object, you can get various information about the file.

The following is a list of all the properties and file objects related to:

file.closed Returns true if the file has been closed, otherwise false.
file.mode return to the open file access patterns.
file.name returns the name of the file.
If the print output file.softspace used, must be followed by a space character, returns false. Otherwise, it returns true.

close () method

close File object () method to refresh the information has not written any buffer, and close the file, after which they can no longer be written.

When a file object reference is re-assigned to another file, Python will close the file before. Close the file with a close () method is a good habit.

write () method

write () method writes the string may be any of an open file. It is important to note that, Python strings can be binary data, rather than just text.

write () method does not add a newline character ( '\ n') at the end of the string:

read () method

read () method reads a character string from an open file. It is important to note that, Python strings can be binary data, rather than just text.

Published 16 original articles · won praise 0 · Views 275

Guess you like

Origin blog.csdn.net/JOKERSNEAKER/article/details/105282128