File handling - Writing
Writing data to file
When working with files, three main commands are used to handle data safely and correctly:
- open(“filename”, “mode”) – Opens or creates a file ready for use
- “w” writes (overwrites existing data)
- “r” reads (opens for reading only)
If the file doesn’t already exist, using “w” will create it automatically
- write(“text”) – Sends text from your program into the file
- Everything written must be a string
- Use “\n” to move to a new line in the file
- close() – Finishes working with the file and saves all changes