Comprehensive File handling python notes class 12 PDF

In this article Comprehensive Notes on file handling python notes class 12, we will provide you with an introduction to file handling and text files in python.

Introduction to file handling python notes class 12

As we know, Files are an essential part of computers. Everything stored on the computer is saved in files. These files are a collection of bytes stored in computers. Data can be represented in specific forms in files. These files can be accessed at any time. These files are saved with an extension or file types. They need a specific program to read data and write data. 

For Ex. Documents can be opened and read through Word Processing software such as MS Word, OO Writer, Word Star, etc.

File handling is the process of handling data by software including IO operations. These files stored in a directory on a hard drive. Whenever any operation is carried out, the file is opened and make available to write upon on reading purposes on RAM. 
 
In the next section, we will discuss what is the need for a data file in the context of the article – Comprehensive Notes on File Handling in Python class 12.

Need for a data file

The computer has a powerful feature of saving data. As you know data means raw facts and figures. and meaningful data is known as information. These data can be stored in a file. Whenever users need such data, data can be presented by files.
 
When you develop a program in python, the final output of the program is not available for future use. Data files serve this purpose.
 
 They can store the output in the following files and those files we will use in file handling python notes class 12:
 
  1. Text Files: It has an extension .txt. It can be directly opened by a text editor like notepad, Wordpad, etc. It is capable to handle textual data and information. The text files contain a series of lines. A line is a set of characters or strings. These characters can be in ASCII or UNICODE form. In the text files, each line is terminated by ‘EOL-End Of Line’ i.e ‘/n’ in python. 
  2. Binary Files: Binary files are capable to store large files such as images, videos, audio files, etc. These files have a specific pattern to read data in the correct type. These files don’t have any delimiter. They are easier and faster than text files. In binary files data interpreted by correct data type. Python provides specific functions to handles these data types and data files.
  3. CSV (Comma Separated Values) Files: It is a plain text file that contains a list of data. CSV files can be opened and operated by MS Excel and allow to export and import data. It can handle big data.  All the values are separated by a comma.

In the next section of Comprehensive Notes on file handling python notes class 12 data file operations are listed out:

Data file operations

The following tasks will be performed on data files.
  1. Creation of files
  2. Opening files
  3. Reading files
  4. Writing files
  5. Appending data in files
  6. Deleting Data from files
  7. Creating copy 
  8. Updating files data

Have a look at text files for Comprehensive Notes on file handling python notes class 12. 

Text Files

Open a text file:
Syntax:<file object> = open(file_name,access_mode)
file object : It is just like a variable or object
open(): It is a function with two parameters. 
  1. file_name: It accepts a file name with .txt extension.
  2. access_mode: It specifies the mode to access the file. The default mode is reading mode. These modes are a) r: to read a file        b) w: to write            c) a: append  contents
Note: 
1. ‘+’ sign is used to open the file for both modes of reading and writing after access_mode. When it is used the file pointer will be at the beginning of the file
2. ‘b’ along with the above modes to work with binary files 
Example: 
f = open(“MyFile.txt”,”r”) – Open file for reading
f = open(“MyFile.txt”,”w”)  – Open file for writing

In the next section of Comprehensive Notes on file handling python notes class 12 you will learn how to read the data from text file. 

Reading file

Python provides the following functions for reading files:
1) read(): To read the whole file, take a look at the following example.
 
Reading text file using read function- File Handling python class 12
File Handling python class 12
2) read(n): It read n number of characters from the beginning of the file. If the file contains less than n number of characters then it will read file up to EOF (End Of File).
reading n no. of characters using read function in python
reading n no. of characters using read function in python
 
3) readline(): It read a line from starting the place of the cursor where it’s placed up to EOL (End of Line). 
 
use readline function in python file handling class 12
use readline function in python file handling class 12

4) Reading the entire file using readline(): 
reading whole file using readline function in python
reading  whole file using readline function in python

 
5) readlines(): To read specific lines from text file
reading whole file using readlines
reading whole file using readlines

6) Reading specific lines:
reading specific lines in python file handling
reading specific lines in python file handling
 
When a variable is assigned with fileobject.readlines() it stores no. of lines in a list with starting index 0(zero).
 
Watch these videos for practical understanding:

In the next section of Comprehensive Notes on file handling python notes class 12, you will learn how to write data into tex files. 

Writing to File

The following functions are used to writing into files.
1. write(): It takes a string as a parameter to write in the file. ‘n’ is used to specify the end of the string.
using write function in python
using write function in python


contents of text file using write function in python
contents of text file using write function in python
2) writelines(): This method is used to write a sequence of lines, strings, tuples, etc in a file. 
writing multiple lines using writelines function in python
writing multiple lines using writelines function in python

Four lines created in a list l and written in a text file using writelines() function.

Watch the following video lesson for more practically understanding :
 

The next section of Comprehensive Notes on file handling python notes class 12 is dedicated to the manipulation of data. 

Manipulation of Data

Program 1. Counting lower case characters from a text file
counting lowercase characters form text file in python
counting lowercase characters from text file in python
 
Program 2. Counting words from a text file
counting no of words from text file
counting no of words from text file
 
Program 3. Counting no. lines starting with ‘S’ in the file
program counting line starting with S in python
program counting line starting with S in python
Program 4. Count occurrences of a specific word in text file (Exact Match)
program - count Occurrence of a word in a text file
program – count Occurrence of a word in a text file

Watch the following video for practical understanding:

Tell and Seek functions

Follow the below-given link to download the file of the tell and seek function. 

tell() and seek()

Download the file

Watch this video for practical understanding:

Follow this link to access important questions and answers for text file:

File handling in python class 12 important questions

Follow this link to access important practical programs for the practical file on the topic file handling class 12:

File Handling programs for practical file class 12

Watch the following video for some more programs:

Access computer science contents from here

So I hope you enjoyed the article – Comprehensive Notes on file handling python notes class 12 and learn something from it.

Share this article Comprehensive Notes on file handling python notes class 12 with your friends and hit the like button. If you have any doubt or query, you are free to ask in the comment section.

Recommended: Computer Science Class 12

Download the file as pdf from below given link.

11 thoughts on “Comprehensive File handling python notes class 12 PDF”

  1. Great and worth appreciable work. Good source for begginers to understand a lot more.

    #Pythogen

  2. Pingback: Importing/Exporting Data between CSV files and Dataframes - TutorialAICSIP

  3. Pingback: Class 12 Important Questions - File Handling - Computer Science with Python - TutorialAICSIP

Comment Your Views