close()
The file method that finishes with a file, writing out any buffered data and freeing it for other programs.
In more detail
Text written to a file may sit in a memory buffer until close, so a program that forgets to close can leave the file empty or incomplete. A with statement closes the file for you, even if an error happens.
Related terms
- file object — The object returned by open() that you read from or write to, and close when finished.
- with statement — A statement that sets something up, runs a block, and guarantees the matching clean-up afterwards, most often closing a file.
Taught in
- What A Program Really Is — Hands-on Python: Complete Video Course & Book
- What Python Is, And Where It Came From — Hands-on Python: Complete Video Course & Book
- Installing Python On Windows — Hands-on Python: Complete Video Course & Book
- Sample Graphics Programs — Hands-on Python: Complete Video Course & Book
- Files: Writing And Reading — Hands-on Python: Complete Video Course & Book
- Paths And Files The Modern Way — Hands-on Python: Complete Video Course & Book