Question 1 of 10intermediate💡 UnderstandShort Answer2 marks

For a newly created file, is there any difference between write() and append() methods?

Correct Answer

No, there is no difference between write() and append() methods for a newly created file.

When a file does not exist, both write mode (w) and append mode (a) create a new empty file. In both cases, the data is written from the beginning of the file since there is no existing content.

The difference between these modes becomes relevant only when the file already exists. In write mode, existing contents are overwritten and the file offset is at the beginning of the file. In append mode, new data is added at the end of existing data and the file offset is at the end of the file.

Exercise: Think and Reflect | Q: 1 | (Page 6)
For More Understanding

Explanation

The textbook explicitly states that 'In both cases, if the file does not exist, then a new empty file will be created.' This means for a newly created file, both modes behave identically. The distinction only matters for existing files - write mode overwrites from the beginning while append mode adds to the end. The file offset position (beginning for write, end for append) is irrelevant when the file is empty/new.