Practice mode Python

What happens when open(‘data.txt’, ‘x’) is called and data.txt already exists?

Correct answer C. FileExistsError is raised

Explanation

Mode 'x' means exclusive creation: it creates a new file and fails with FileExistsError if the file is already there. This protects existing data from being overwritten by mistake, unlike 'w', which silently truncates an existing file to zero length.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.