Python basics: file read/write

Source: Internet
Author: User
Because the speed of CPU and memory is much higher than the speed of peripherals, in IO programming, there is a serious issue of speed mismatch. For example, if you want to write 0.01 MB of data to a disk, it takes only seconds for the CPU to output MB of data, but it may take 10 seconds for the disk to receive the MB of data. what should you do? There are two ways: The first is to wait for the CPU, that is, the program suspends the execution of subsequent code, and wait for M of data in 10 seconds because the CPU and memory speed is much higher than the speed of peripherals, so, in IO programming, there is a serious mismatch of speed. For example, if you want to write 0.01 m of data to a disk, it takes only seconds for the CPU to output m of data, but it may take 10 seconds for the disk to receive the M data. what should you do? There are two ways: The first is to wait for the CPU, that is, the program suspends the execution of subsequent code, wait for M of data to be written to the disk after 10 seconds, and then proceed to execute, this mode is called synchronous I/O. The other method is that the CPU does not wait, but tells the disk, "you write slowly, don't worry, I am going to do something else, subsequent code can be executed immediately. The difference between asynchronous IO synchronization and asynchronous mode is whether to wait for the results of IO execution. The advantage of asynchronization is high procedural performance, but the disadvantage is that the programming model is complex. You have to know when to notify you that "Hamburg is ready", and the methods for notifying you are different. If the waiter runs to find you, this is the callback mode. if the waiter sends a text message to notify you, you have to constantly check your mobile phone. this is the polling mode. In short, the complexity of asynchronous I/O is much higher than that of synchronous I/O. for example, if you go to McDonald's for a meal and you say "come to a hamburger", the waiter tells you, sorry, the hamburger needs to be made now and it will take five minutes, so you stood in front of the cashier and waited for five minutes to get the hamburger and then go to the mall. this is synchronous I/O. When you say "come to Hamburg", the waiter tells you that it takes five minutes to wait for the hamburger. you can go to the mall and wait until it is ready. we will notify you again, in this way, you can immediately do other things (shopping malls), which is asynchronous IO. 9.1 file read/write #! /Usr/bin/pythonpoem = ''' \ 1. dog2.cat3. rabbit ''' f = file ('/tmp/poem', 'w') f. write (poem) f. close () # output file content. Method 1 # 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 () writes files to write files are the same as read files. The only difference is that when the open () function is called, the input identifier 'W' or 'WB 'indicates 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 the file, but you must call f. close () to close the file. When writing a file, the operating system usually does not immediately write data to the disk, but caches the data in the memory and writes the data slowly when it is idle. Only when the close () method is called Can the operating system ensure that all data not written to the disk be written. The consequence of failing to call close () is that the data may only be written to the disk, and the rest will be lost. Therefore, we still use the with statement to insure: with open ('/Users/michael/test.txt', 'w') as f:
F. write ('Hello, world! ')
Two advantages of using close (): 1. number of opened files disabled 2. make sure that the data in the memory is written to the hard disk. flush () can write the content in the memory to the hard disk summary in Python without shutting down the file. file reading and writing is done through the file object opened by the open () function. It is a good habit to use the with statement to operate on file IO.

The above is the basics of Python: file read and write details, please pay attention to other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.