First Program
A = open (' Test.txt ', ' W ')
A.write (' JDHFJKF ')
A.write (' \ n ')
A.write (' ZBVBVXSG ')
A.close ()
A 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 feel like they should be the same. But why only the first program can write to Test.txt, and the second program execution result is blank?
(I think: is not a file only with open and ' w ' once, otherwise, every time ' W ' will be written in front of the things empty. But the problem came again, I took the second program fourth line out, the third line can be written, but with the fourth line, and become blank) this I understand
New problem: In the first program, a as a variable, also ' W ' ah, every time the execution of A.write (), is not the same as the second program? should also be every write once, the front of the empty?
Reply content:
Each time the W open file clears the file, you see, I've never used Python before, and I've been looking through the document since I saw you.
Open () Returns a file object, and is the most commonly used with the Arguments:open (filename, mode).
>>>
>>> f = open('workfile', 'w')>>> print f
There are two modes of writing a file: one called truncate (truncated) and one called Append (append). The former each time the file is opened to delete the contents of the file, and then write the content, the latter each time the file is opened does not delete the existing content, but after the content is written after the content. The rest of the main questions to think about?
========================================================================
Learn things, or good first to find a few books, the foundation to play well. Look at the document. The answer to the new question raised by the topic, the empty content is not what happens when file Objects calls write, but what happens when you use the "W" mode open file.
The main problem is that there are doubts in two places, the characteristics of the "w" pattern, and the role of File.close.
Separate instructions.
About the File.close,python document is very clear, see here: File.write
, I quote
Due to buffering, the string could not be actually show up in the file until the flush (
) or close (
) method is called.
As for W mode, the Python document did not clear the story, or I did not see it: I saw an excellent description on StackOverflow: Python open built-in function:difference between Modes A, A +, W, w+, and r+?
We can see that the "W" mode problem is not really related to Python, to understand its characteristics, it needs to be traced back to the C standard library
the 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've never seen you do this before.
I don't understand how you put the "Open → read/write → Close" process into this way. I understand that opening the same file multiple times is not a behavior that should occur.
def writeFile() : file = open('test.txt','w+') file.write('testtesttest') file.write('\n') file.write('new line') file.close()
Open the parameters of the function should have a corresponding explanation, a simple question in the words:
' W ' opens and empties the file and re-creates if there is no file.
If you need to be able to write multiple times, you need to use the ' a ' parameter. If you change the ' W ' of your second piece of code to ' a ', you'll find that you're not getting the results you expect. What is the reason? In fact, the writing location of the file is very ingenious, a bit similar to the C language of the pointer operation. And each time it is reopened, the pointer is re-zeroed. If the first string you write is a long one, you will find that you still have a portion of the information left.
Simply write the example below:
as the code above, the file is emptied after each opening. Finally, the 6th line of code clears the file. As the code above, the file is emptied after each opening. Finally, the 6th line of code clears the file.
The results are as follows:
If you switch to ' a ', then each time you reopen it, write it from the beginning of the file.
The code and the results are as follows (note that the string of letters that I intentionally write tttttt, since the position of each write is the beginning, but the length of the write is not enough, so the following TTTT characters are not overwritten):
Look at an example and change the append parameter to ' A + ' (so that the "pointer is written" remains at the last position):
Foundation is too important, only open not close is in bullying. Recommended to learn to learn C + +, look at the remote noon file handle only open not close what is the case. The answer is as you say, each time you open a file with open (' Test.txt ', ' W '), the front is emptied. If you're not sure what's going on in the process, you can print it out 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()