Python file read-write-file r+ open read/write actual performance

Source: Internet
Author: User

First, the conclusion:

File r+ Open:

1. Write () cannot implement insert write, it always overwrites write or append write;

2. If the file is open as write (), overwrite the write from the beginning;

3. If the file is open, use F.seek () to specify the position of the file pointer, and then execute F.write () to write from the pointer position (overwrite write);

4. If the file is opened, execute the ReadLine () before executing write (), the implementation is additional write

File A + open:

1. When the file opens, the start pointer is the end of the file, and ReadLine () will not read the data

2. Write () is always attached, even if the file pointer is in the middle and the write () is still attached

Common:

1. Write () is write-first memory, F.close () will be physically written

2. The F.tell () function treats the ' \ n ' newline as a character instead of two, and the offset is 1, not 2.

Test process:

File Companies.txt content:

Googletencentalibababaidu

r+ mode:

# python version:3.6.3
>>> f = Open (r'Python/file/companies.txt','r+')>>> F.tell ()#view file pointer values0>>> F.readline ()#read a line'google\n'>>> F.tell ()#view file pointer values again7>>> F.write ('XXX')#attempt to write string ' xxx ' at file pointer location of 7 o'clock3>>> F.tell ()#View the file pointer value again, has changed to the end of the file31>>> F.readline ()#read another line to see if you read EOF or read the second line .'tencent\n'>>> F.readlines ()#actually read the second line, did not start reading from the file pointer, and the second line does not have the previous write ' xxx ', then ' xxx ' write to where? Read out all the rest of the line .['alibaba\n','Baidu']>>> F.seek (0,0)#the remaining lines also did not find ' xxx '. Is it attached to the back of the first line? Point the file pointer to the beginning through Seek (0,0)0>>> F.readlines ()#read the entire file['google\n','tencent\n','alibaba\n','baiduxxx']>>>#the originally written ' xxx ' is actually appended to the end of the file. After opening the file with r+, the F.readline () is used to read the line, and then f.write (' xxx ') cannot write the string to the position of the file pointer, and after writing, the memory is written and not actually written into the file, when ReadLines () is not readable. , ReadLines () can be read after executing F.seek (). Read-out also does not represent commit, the actual physical write is performed f.close () only occurs>>> F.seek (0,0)#position the file pointer back to the beginning0>>> F.write ('BBB')#The file pointer writes the string using the Write () method at the beginning of the initial state3>>> F.tell ()#View the file pointer position, after write () processing, is the pointer again to the end? 3>>> F.seek (0,0)#after discovering this write (), the pointer does not refer to the end, but at the beginning of the file. Then point the pointer back to 0, then read the whole file again, see the effect of writing0>>>f.readlines () ['bbbgle\n','tencent\n','alibaba\n','baiduxxx']>>>#' BBB ' is written at the beginning of the file and is overwritten, not inserted. >>>ImportOS>>>Print(OS. Seek_set,os. Seek_cur,os. Seek_end)#the starting position of the file, the current position, the end position constant0 1 2>>> f.seek (5,os. Seek_set)#point the file pointer directly to 55>>>F.tell ()5>>> F.write ('TTT')#try again to see the effect of adjusting the pointer position and then write () without executing readline3>>>F.tell ()8>>>F.seek (0,0) 0>>>f.readlines () ['bbbgltttencent\n','alibaba\n','baiduxxx']>>>#' TTT ' was written from the 5th place! Note Write () can also implement overwrite write in the middle of the file, not only at the beginning overwrite the write or the end of the file append write, but only if the file cannot be readline after opening, but to set the pointer position directly after calling write ()>>>#Summary: In r+ mode, write () cannot implement insert write, it always overwrites write or append write. If the file is open as write (), the write is overwritten from the beginning, such as when the file is opened, ReadLine () is executed, and then write () is implemented with an additional write

A + mode:

>>> f = Open (r'Python/file/companies.txt','A +')#A + open>>> F.tell ()#Initial pointer position28>>>#The initial pointer has pointed to the end>>> F.readline ()#read a line, EOF, cannot read data"'>>> F.write ('AAA')#Write Data3>>> F.seek (0,0)#pointer Reset0>>> F.readlines ()#Read all Rows['google\n','tencent\n','alibaba\n','baiduaaa']>>>#the ' AAA ' is appended to the end. This is different from r+ open file, r+ Open file after write () is overwritten from the beginning>>> F.seek (5,0)#The file pointer is offset forward from the file start position 55>>>F.tell ()5>>> F.write ('BBB')#Try writing again, whether it's starting at position 5 or attaching from the end3>>> F.seek (0,0)#Reset pointer0>>> F.readlines ()#Read all Rows['google\n','tencent\n','alibaba\n','baiduaaabbb']>>>#' BBB ' appended at the end, indicating that even if the file pointer location is specified, a + mode will not write from the middle of the file, always append the write

Python file read-write-file r+ open read/write actual performance

Related Article

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.