What is the use of file handling in Python?
Python provides us with an important feature for reading data from the file and writing data into a file
Mostly, in programming languages, all the values or data are stored in some variables which are volatile in nature.
Because data will be stored into those variables during run-time only and will be lost once the program execution is completed.
Hence it is better to save these data permanently using files.
Best Python Training Institute in Gurgaon...
0.1.1 How Python Handle Files?
If you are working in a large software application where they process a large number of data, then we cannot expect those data to be stored in a variable as the variables are volatile in nature.
Hence when are you about to handle such situations, the role of files will come into the picture.
As files are non-volatile in nature, the data will be stored permanently in a secondary device like Hard Disk and using python we will handle these files in our applications
Types Of File in Python
There are two types of files in Python and each of them are explained below in detail with examples for your easy understanding.
They are:
Binary File
Text File
Binary File in Python
Document files: .pdf, .doc, .xls etc.
Image file: .png, .jpg, .gif, .bmp etc.
Video files: .mp4, .3gp, .mkv, .avi etc.
Text Files in Python
Web standards: html, xml, css, json etc.
Source Code: C, app, py, js, java etc.
Documents: txt, tex, rtf, etc.
0.1.2 Python File Handling Operations
Most importantly there are 4 types of operations that can be handled by Python on files:
Open
Read
Write
Close
file_object = open(file_name, mode)
file_name is the name of the file or the location of the file that you want to open, and file_name should have the file extension included as well.
While using text files, The mode in the open function syntax will tell Python as what operation you want to do on a file.
‘r’ – Read Mode: Read mode is used only to read data from the file.
‘w’ – Write Mode: This mode is used when you want to write data into the file or modify it. Remember write mode overwrites the data present in the file.
‘a’ – Append Mode: Append mode is used to append data to the file. Remember data will be appended at the end of the file pointer.
While using binary files, we have to use the same modes with the letter ‘b’ at the end. So that Python can understand that we are interacting with binary files.
‘wb’ – Open a file for write only mode in the binary format.
‘rb’ – Open a file for the read-only mode in the binary format.
‘ab’ – Open a file for appending only mode in the binary format.
Example1: read a data.txt file
fh = open("/home/eddy/test/data.txt", "r")
print(fh.read())
This is Python Training.
I am Eddy Grant
Thanks For reading.
print(fh.readline())
This is Python Training.
print(fh.readlines())
['This is Python Training.\n', 'I am Eddy Grant\n', 'Thanks For reading.\n']
Example2: Write a new text with python programme.
filename = input("Enter filename with path: ")
fh = open(filename, "w")
data = input("Enter data what do you want to write: ")
fh.write(data)
fh.close()
Enter filename with path: /home/eddy/test/newdata.txt
Enter data what do you want to write: Hey i am eddy Grant this is a test file
verify using cat command file is created
!cat /home/eddy/test/newdata.txt
Hey i am eddy Grant this is a test file
Example3: Read a binary file
fh = open("/home/eddy/Pictures/code.jpg", "rb")
print(fh.readline())
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x 00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03\x04\x06\x04\x04\ x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n'
Get More Information About Python Training and Certification Course Visit Here.
Thank You For Reading
Written By Sachin Saini
Comments
Post a Comment