python file reading

file = open(“testfile.txt”,”w”) 
 
file.write(“Hello World”) 
file.write(“This is our new text file”) 
file.write(“and this is another line.”) 
file.write(“Why? Because we can.”) 
 
file.close() 

Here is what the above code is Doing:
1. We open the file in write mode.
2. We write some text to the file.
3. We close the file.

When we open the file in write mode, the file contents are deleted and we are ready to write new content to the file.

If we open the file in append mode by using “a” instead of “w”, the file contents are preserved and we can write new content to the end of the file.