WEBVTT

00:00.000 --> 00:05.000
File Handling in Python

00:05.000 --> 00:12.000
In this video, we will understand file handling in Python using read, write, and append modes.

00:12.000 --> 00:16.000
Practice reading file contents using Python's open function.

00:16.000 --> 00:20.000
Apply file handling to manage library records.

00:20.000 --> 00:27.000
File handling is the process of creating, reading, writing, and updating files using a programming language.

00:27.000 --> 00:33.000
In Python, file handling enables programs to store and retrieve data permanently.

00:33.000 --> 00:42.000
It supports different modes, read, opens a file for reading, write, creates or overrides a file, append,

00:42.000 --> 00:45.000
adds new data without erasing existing content.

00:45.000 --> 00:52.000
This is useful for managing records, logs, and structured data.

00:52.000 --> 00:54.000
Read mode

00:54.000 --> 00:59.000
If you want to read and display the file content, you can use the read mode like this.

00:59.000 --> 01:06.000
The open function opens library records.txt in read mode, which means the file must exist.

01:06.000 --> 01:09.000
Otherwise, an error will occur.

01:09.000 --> 01:14.000
The read method retrieves the entire content of the file as a string.

01:14.000 --> 01:19.000
The print function outputs the file content to the console.

01:19.000 --> 01:26.000
The width statement ensures the file is properly closed after reading, preventing memory leaks.

01:26.000 --> 01:34.000
This function helps retrieve stored library records, visitor details, and any new visitor entries.

01:34.000 --> 01:37.000
Write mode

01:37.000 --> 01:44.000
The function save library records opens the file, library records.txt in write mode.

01:44.000 --> 01:47.000
This mode overrides any existing content.

01:47.000 --> 01:51.000
It writes book inventories and visitor details to the file.

01:51.000 --> 01:56.000
If the file does not exist, it creates a new one.

01:56.000 --> 01:59.000
Append mode

01:59.000 --> 02:04.000
The function append visitor entry opens the file in append mode.

02:04.000 --> 02:08.000
Instead of overwriting, it adds new visitor details to the file.

02:08.000 --> 02:14.000
If the file does not exist, Python creates it automatically.

02:14.000 --> 02:17.000
Displaying result

02:17.000 --> 02:22.000
The output of this program consists of two parts, console output.

02:22.000 --> 02:29.000
When the program runs, it prints messages confirming that data has been saved and updated.

02:29.000 --> 02:31.000
File output

02:31.000 --> 02:36.000
A file named library records.txt is created or overwritten if it already exists.

02:36.000 --> 02:38.000
It contains

02:38.000 --> 02:46.000
The library book inventories and visitor details are saved first, followed by new visitor entries added in append mode.

02:46.000 --> 02:56.000
This program demonstrates file handling by storing library records, including book inventories and visitor details, using write and append modes.

02:56.000 --> 03:00.000
It also allows reading stored data for reference.

