For python, why are the running results different from those of the two programs?

Source: Internet
Author: User
Summary, w). write (first program of zbvbvxsg
A = open('test.txt ', 'w ')
A. write ('jdhfjkf ')
A. write ('\ n ')
A. write ('zbvbvxsg ')
A. close ()
Second Program
Open('test.txt ', 'w'). write ('jdhfjkf ')
Open('test.txt ', 'w'). write (' \ n ')
Open('test.txt ', 'w'). write ('zbvbvxsg ')
Open('test.txt ', 'w'). close ()
I think they should be the same. But for example, the first program can be written to test.txt, And the execution result of the second program is blank?

(I think: Is it true that only open and 'w' are used for a single file? Otherwise, every 'W' operation clears the previously written content. But the problem comes again. I removed the fourth line of the second program, and the third line can be written, but the fourth line is blank again.) I understand this.

New question: in the first program, a is also a variable with 'W'. Isn't it the same as the second program when a. write () is executed? Should we also clear the previous one every write time? Reply: Every time w open files are cleared, you can see that I have never used Python. After seeing this problem, I checked the document.

Open ()Returns a file object, and is most commonly used with two arguments: open (filename, mode ).


>>>

>>> f = open('workfile', 'w')>>> print f
   
There are two file writing modes: truncate and append ). The former deletes the existing content of the file each time it opens, and then writes the content. The latter does not Delete the existing content each time it opens the file, but writes the content after the existing content. Do you think about the remaining question subjects?
========================================================== ======================================
If you want to learn something, find a few books first and lay the foundation well. Read the document. The answer to the new question first raised by the subject. Clearing the content does not occur when File Objects calls write, but when an open File is opened in "w" mode.

The subject has doubts in two places, the features of the "w" mode, and the role of file. close.
Separate description.
For more information about file. close, see file. write. For reference
Due to buffering, the string may not actually show up in the file until the flush () Or close () Method is called.
As for the w mode problem, the Python document did not clarify this matter, or maybe I did not see it :). I have seen an excellent note on stackoverflow: python open built-in function: difference between modes a, a +, w, w +, and r +?
From this we can see that the "w" mode is not very relevant to Python. To understand its features, we need to trace back to the C standard library. Fopen () function, defined as follows
"W''truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
I have never seen you do this.
I don't know how you understand the process of "open> read/write> close" as this. In my understanding, opening the same file multiple times should not occur.

    def writeFile() :        file = open('test.txt','w+')        file.write('testtesttest')        file.write('\n')        file.write('new line')        file.close()
Each Parameter Function of open should be explained accordingly, which is described below:
'W' open and clear the file. If there is no file, create it again.

If you need to write data multiple times, you need to use the 'A' parameter. If you change the 'W' of your second code to 'A', you will find that the expected results are not displayed. Why? In fact, the file write location is very clever, a bit similar to the C language pointer operation. After each re-opening, the pointer is reset to zero. If the first string you write is long, you will find that some information is retained.
An example is as follows:
The code above clears all files after each opening. Finally, 6th lines of code clears the file. The code above clears all files after each opening. Finally, 6th lines of code clears the file.
The result is as follows:

If it is changed to 'A', the data is written from the beginning of the file every time it is re-opened.
The code and results are as follows (note that tttttt, the string that I intentionally wrote later, is not long enough because the write position is the start time, therefore, the subsequent tttt and other characters are not overwritten ):


Let's look at another example and change the append parameter to 'a + '(in this way, the written "pointer is always at the previous position "):

The Foundation is too important, but open or close is playing a rogue game. We recommend that you study C/C ++ to see what happens when the file handle is open and not close. The answer is as you said. When you open the file with open('test.txt ', 'w'), the previous one is cleared. If you do not know what happened in this process, you can add print under each write.

open('test.txt','w').write('jdhfjkf')print open('test.txt','r').readlines()open('test.txt','w').write('\n')print open('test.txt','r').readlines()open('test.txt','w').write('zbvbvxsg')print open('test.txt','r').readlines()open('test.txt','w').close()

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.