Computer Science – File handling | e-Consult
File handling (1 questions)
Write mode (often denoted as 'w') opens a file for writing. If the file already exists, its contents are overwritten. If the file does not exist, a new file is created.
Append mode (often denoted as 'a') opens a file for writing. New data is added to the end of the existing file content without overwriting it. If the file does not exist, a new file is created.
Example of 'w' mode: You would use 'w' mode when you want to replace the entire content of a file with new content. For instance, overwriting a configuration file with a new set of settings.
Example of 'a' mode: You would use 'a' mode when you want to add new data to the end of an existing file. For example, logging events to a log file, where each new event is appended to the end of the log.
| Mode | Description |
| 'w' (Write) | Overwrites existing content or creates a new file. |
| 'a' (Append) | Adds new content to the end of the existing file. |