file object
The object returned by open() that you read from or write to, and close when finished.
In more detail
It has methods such as read, readline, readlines, write and close, and it can be looped over one line at a time. Opening it in a with statement closes it automatically. Beginners often mix up three things: the file name, the file object and the text read from it.
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 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.
- with statement — A statement that sets something up, runs a block, and guarantees the matching clean-up afterwards, most often closing a file.
- close() — The file method that finishes with a file, writing out any buffered data and freeing it for other programs.
Taught in
- First Run, And Fixing A Broken Install — Hands-on Python: Complete Video Course & Book
- Files: Writing And Reading — Hands-on Python: Complete Video Course & Book
- Chapter Two In One Sitting — Hands-on Python: Complete Video Course & Book
- Paths And Files The Modern Way — Hands-on Python: Complete Video Course & Book
- JSON And CSV — Hands-on Python: Complete Video Course & Book
- Streams, Handles And Open Modes — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep