Since the speed of CPU and memory is much higher than the speed of peripherals, there is a serious speed mismatch problem in IO programming. For example, if you want to write 100M data to the disk, it only takes 0.01 seconds for the CPU to output 100M data, but it may take 10 seconds for the disk to receive the 100M data. What should I do? There are two methods: The first is that the CPU waits, that is, the program pauses the execution of subsequent code, waits for 100M of data to be written to the disk after 10 seconds, and then continues execution. This mode is called synchronous IO; the other The method is that the CPU does not wait, but just tells the disk, "You always write slowly, don't worry, I will go on to do other things." Therefore, the subsequent code can be executed immediately. This mode is called asynchronous IO synchronization and asynchronous The difference lies in whether to wait for the result of IO execution. The advantage of asynchronous is high program performance, but the disadvantage is that the programming model is complex. Think about it, you have to know when to notify you that "the burger is ready," and the methods of notifying you vary. If the waiter comes to find you, this is the callback mode. If the waiter sends you a text message to notify you, you have to keep checking your phone, which is the polling mode. In short, the complexity of asynchronous IO is much higher than that of synchronous IO. It's like when you go to McDonald's to order food and you say "have a burger", the waiter tells you, sorry, the burger needs to be cooked fresh and you need to wait 5 minutes, so you stand at the checkout counter. I waited for 5 minutes before getting the burger and then went to the mall. This is synchronous IO. You say "have a burger" and the waiter tells you that the burger needs to wait for 5 minutes. You can go to the mall first. When it is ready, we will notify you so that you can go do other things immediately (go to the mall). It is asynchronous IO. 9.1 File reading and writing #!/usr/bin/pythonpoem = '''\1.dog2.cat3.rabbit'''f=file('/tmp/poem','w')f.write(poem)f. close() #Output file content, method one #f=open('/tmp/poem','r')#for line in f.readlines():# print(line.strip())#f.close( )
#Output file content, method 2 f=file('/tmp/poem')while True: line = f.readline() if len(line)==0: break print(line.strip()) f.close() Writing a file is the same as reading a file. The only difference is that when calling the open() function, passing in the identifier 'w' or 'wb' means writing a text file or writing a binary file:>> > f = open('/Users/michael/test.txt', 'w')
>>> f.write('Hello, world!')
>>> f.close() You can call write() repeatedly to write to the file, but be sure to call f.close() to close the file. When we write a file, the operating system often does not write the data to the disk immediately, but caches it in the memory and writes it slowly when it is free. Only when the close() method is called, the operating system guarantees that all unwritten data will be written to the disk. The consequence of forgetting to call close() is that only part of the data may be written to the disk, and the rest is lost. Therefore, it is better to use the with statement to be safe: with open('/Users/michael/test.txt', 'w') as f:
f.write('Hello, world!')
Use close( ) two benefits: 1. Close the number of open files 2. Ensure that the data in the memory is written to the hard disk f.flush() can write the contents of the memory to the hard disk without closing the file Summary In Python, file reading and writing are This is done through the file object opened by the open() function. It is a good habit to use the with statement to operate file IO.
The above is the detailed content of Python basics: file reading and writing. For more information, please follow other related articles on the PHP Chinese website!