This will be a short lesson and the end of my learning to code in Python roadmap series 🥳🎉🎉
When opening files, you used 2 arguments with the open() function; The filename followed by the "r" to open the file for reading. In this lesson, you will see 2 other arguments you can give.
w |
opens the file for writing. But overwrites all previous file data |
|---|---|
a |
opens the file for writing. But appends new data to the end of the file |
<aside>
💡 TIP - Python will look for the file in the same directory as where your .py file is saved. If it cannot find the file in the same directory, it will create a file with the filename given as an argument.
</aside>
w parameterhttps://replit.com/@bitFez/writing-to-file-example-1#main.py
a parameterhttps://replit.com/@bitFez/writing-to-file-example-2#main.py
Did you notice that the data was added to the end of line 3?
https://youtube.com/shorts/Zrbk9VLjGPQ?si=J7Ndow56cOYOAU4D
.write() commandThis will add a line to your file as a string datatype. It will not add a new line break at the end of a line. You need to do that manually with '\n
'\n''\n'