Skip to content

Computer Science · Ch 2 — File Handling in Python

Opening and Closing a Text File

2.3

Opening and Closing a Text File

Opening and Closing a Text File

In real-world applications, programs work with data arriving from many different sources — databases, CSV files, HTML, XML, JSON feeds, and more. Broadly, a program accesses a file either to write data into it or to read data out of it, but the full set of operations spans creating a file, opening it, writing to it, traversing through it, reading from it, and finally closing it. Python provides the io module, which contains the functions used to carry out all of these file-handling operations. …