File Handling in Python Class 12 Notes

Teachers and Examiners (CBSESkillEduction) collaborated to create the File Handling in Python Class 12 Notes. All the important Information are taken from the NCERT Textbook Computer Science (083) class 12.

File Handling in Python Class 12 Notes

File Handling in Python

File handling is the process of saving data in a Python programme in the form of outputs or inputs, The python file can be store in the form of binary file or a text file. There are six different types of modes are available in Python programming language.

1. r – read an existing file by opening it.
2. w – initiate a write operation by opening an existing file. If the file already has some data in it, it will be overwritten; if it doesn’t exist, it will be created.
3. a – Open a current file to do an add operation. Existing data won’t be replaced by it.
4. r+ – Read and write data to and from the file. It will replace any prior data in the file.
5. w+ – To write and read data, use w+. It will replace current data.
6. a+ – Read and add data to and from the file. Existing data won’t be replaced by it.

File Handling in Python Class 12 Notes

In Python, File Handling consists of following three steps

  • Open the file
  • Read and Write operation
  • Close the file

Opening and Closing file in Python

The file must first be opened before any reading, writing, or editing operations may be carried out. It must first be opened in order to create any new files. The file needs to be closed after we are finished using it. If we forgot to close the File, Python automatically closes files when a programme finishes or a file object is no longer referenced in a programme.

Example
open()
close()

File Handling in Python Class 12 Notes

Create a file using write() mode in Python

The write() mode is used to creating or manipulating file in Python.

Example
# Create a file in python
file = open(‘example.txt’,’w’)
file.write(“I am Python learner”)
file.write(“Welcome to my School”)
file.close()

Read a file using read() mode in Python

You can read the data from binary or text file in Python using read() mode.

Example
file = open(‘example.txt’, ‘r’)
print (file.read())

Write a file using append() mode in Python

You can add multiple line of data in the file using append() mode in Python.

Example
file = open(‘example.txt’, ‘a’)
file.write(“The text will added in exicesiting file”)
file.close()

File Handling in Python Class 12 Notes

Write a file using with() function mode in Python

You can also write the file using the with() mode in Python.

Example
with open(“example.txt”, “w”) as f:
f.write(“Welcome to my School”)

Split lines in a file using split() mode in Python

You can also split lines using file handling in Python.

Example
with open(“example.text”, “r”) as file:
data = file.readlines()
for line in data:
word = line.split()
print (word)

File Handling in Python Class 12 Notes

Flush() function in File

When you write onto a file using any of the write functions, Python holds everthing to write in the file in buffer and pushes it onto actual file on storage device a leter time. If however, you want ot foce Python to write the contents of buffer onto storage, you can use flush() function. Python automatically flushes the files when closing them.

Example
f = open(‘example.text’, ‘w+’)
f.write(‘Welcome to my School’)
f.flush()

File Handling in Python Class 12 Notes

Difference between Text file and Binary file

Text File – A text file is one whose contents may be examined using a text editor. Simply put, a text file is a collection of ASCII or Unicode characters. Text files include things like Python programmes and content created in text editors.

Binary File – A binary file is one whose content is stored in the form of binary, which consists of a series of bytes, each of which is eight bits long. Word documents, MP3 files, picture files, and .exe files are a few example of binary files.

error: Content is protected !!