file mode
The second argument to open(), which says how the file will be used: "r" read, "w" write from scratch, "a" append, with "b" added for bytes.
Opening an existing file in "w" mode erases its contents immediately and without warning.
In more detail
"w" empties an existing file straight away, so opening the wrong name in write mode destroys its contents. "a" adds to the end instead. Text modes return strings; binary modes such as "rb" return bytes.
Related terms
- open() — The built-in function that opens a file by name and returns a file object, for reading by default or for writing or appending.
- file object — The object returned by open() that you read from or write to, and close when finished.
- bytes object — A Python value holding raw bytes, numbers from 0 to 255, written like b"abc", as opposed to a string of characters.
Taught in
- Files: Writing And Reading — Hands-on Python: Complete Video Course & Book
- Chapter Two In One Sitting — Hands-on Python: Complete Video Course & Book
- More String Methods — Hands-on Python: Complete Video Course & Book
- Paths And Files The Modern Way — Hands-on Python: Complete Video Course & Book