Python File Handling | Class 12 Important Explain 2021

Python File Handling | Class 12 Important Explain 2021
Python File Handling | Class 12 Important Explain 2021

Python File Handling – File Handling is Basically Used to Store Output and Save Data. That Could be Use Later

Basically there are three type of File Handling :

  • Text File
  • CSV File
  • Binary File

Text File

A Text File Stored Data in ASCII (American Standard Code for Information Interchange). Each line of the file is terminated with the Special character is called EOL (End of line). EOL (‘/n’)

Remember In text file not media files included Like – Image, Video, Sound etc.

Opening Text File

For Opening Text File first of all you have to Create a new with the help of python command. Than there are 3 operations or mode which is also work with CSV and Binary File.

  • Reading Data (‘r’)
  • Writing Data (‘w’)
  • Appending Data (‘a’)

Open Command

open(“file.txt”,’w’)

open(“file.txt”,’r’)

open(“file.txt”,’a’)

Example of File Handling Operations or Modes

Close Command

file.close() # this code is work with every mode.

Python File Handling | Class 12 Important Explain 2021

Read() Mode

In Read mode You can read file very easily by using read()

a = open(“file_path_with_extension.txt”, ‘r’)

b = a.read()

Print(b)

a.close()

This is read mode Program

Write Mode

Write mode is use to open file for writing purpose. There are 2 type of create write mode program but Both result Are Same. So I’m show you only one Way to create write mode program. If you already created file and write some thing and than you open file in write mode your all Previous Data is Lost. So Write Mode is always use for new file.

a = open(“file_path_with_extension.txt”, ‘w’)

b = a.write(“Your Text Here”)

Print(b)

a.close()

This is the write mode function

Remember:- Close() Function is very important for closing the file. Close() function is use at the end of program.

Append Mode

Append mode is use write some text in existing file. It is not use for creating new file. If you use append it mean you want to add some more text in your file.

a = open(“file_path_with_extension.txt”, ‘a’)

b = a.write(“your New Text Here”)

print(b)

a.close()

This is append Program.

Leave a Reply

Your email address will not be published. Required fields are marked *