File HandlingLesson 50Free preview
Python Write/Create Files
This lesson is part of Python · All levels
Summary
The "w" mode creates a new file or overlaps the entire contents of an existing file, whereas the "a" mode only adds data at the end. The f.write() function sends text exactly as it is written, including having to write it yourself \n to move the line. To create a file that doesn't exist, use the "x" mode that will error when the file is available.
Key points
- Open(nama, "w"): new files are created, or old files are deleted from the beginning.
- Open(nama, "a"): data is added at the end, the old content remains secure.
- Open(nama, "x"): create a new file; error when the file is already there (FileExistsError).
- F.write() does not add automatic newlines; write "\n" yourself to move the lines.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.