Questions And Answers

More Tutorials

Python Writing And Appending To A File

f is an object for the file. It is not a method or a special character. You will notice me using f.write() or or f.close(), in further description or tutorial, but you can use character or word of your own choice instead.

“w” mode:Here “w” stands for write. After opening or creating a file, a function, f.write() is used to insert text into the file. The text is written inside closed parenthesis surrounded by double quotations. There is a certain limitation to the write mode of the opening file that it overrides the existing data into the file. For a newly created file, it does no harm, but in case of already existing files, the previous data is lost as f.write() overrides it.
“a” mode:“a” symbolizes append mode here. In English, appends mean adding something at the end of an already written document, and the same is the function the mode performs here. Unlike write mode, when we use "a" keyword, it adds more content at the end of the existing content. The same function i.e., f.write() is used to add text to the file in append mode. It is worth noting that append mode will also create a new file if the file with the same name does not exist and can also be used to write in an empty file.
“r+” mode:At the beginning of the description, I told you that we would learn reading and writing a file simultaneously. Well, r+ mode is more of a combination of reading and append than read and write. By opening a file in this mode, we can print the existing content on to the screen by printing function and adding or appending text to it using f.write() function.

If you are writing in append mode, start your text by putting a blank space or newline character (\n) else the compiler will start the line from the last word or full stop without any blank space because the curser in case of append mode is placed right after the last character. So, it is always considered a good practice to adopt certain habits that could help you in the future, even though they are not much helpful now.


f.close() is used to close a file when we are done with it. It is a good practice to close a file after use because whichever mode you opened it for, the file will be locked in for that specific purpose and could not be accessed outside the program, even though the file browser.


 f = open("mockt.txt", "w")
 a = f.write("Mockstacks Text file\n")

 f = open("mockt1.txt", "a")
 a = f.write("Mockstacks 1 Text file\n")

# Handle read and write both
f = open("mockt1.txt", "r+")
f.write("thank you")


In this page (written and validated by ) you learned about Python Writing And Appending To A File . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Seek tell files.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.