Professional Documents
Culture Documents
Experiment No 5
Experiment No 5
05
A.1 Aim:
To explore Files and directories
a) Python program to append data to existing file and then display the entire file
b) Python program to count number of lines, words and characters in a file.
A.2 Prerequisite:
Python Basics
A.3 Outcome:
After successful completion of this experiment students will be,
Illustrate different file handling operations
A.4 Theory:
File:
Files are named locations on disk to store related information. They are used to permanently
store data in a non-volatile memory (e.g. hard disk).
Since Random Access Memory (RAM) is volatile (which loses its data when the computer is
turned off), we use files for future use of the data by permanently storing them. When we want
to read from or write to a file, we need to open it first. When we are done, it needs to be closed
so that the resources that are tied with the file are freed.
A file operation takes place in the following order:
Open a file
Read or write (perform operation)
Close the file
Opening a File:
Python provides an open() function that accepts two arguments, file name and access mode in
which the file is accessed. The function returns a file object which can be used to perform
various operations like reading, writing, etc.
Syntax:
file object = open(<file-name>, <access-mode>, <buffering>)
The close() method:
Once all the operations are done on the file, we must close it through our Python script using the
close() method. Any unwritten information gets destroyed once the close() method is called on a
file object.
Syntax:
fileobject.close()
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board access
available)
Roll. No: B02 Name: Somesh Ashok Bagal
Class: SE Batch: B1
Date of Experiment: 1-apr-2022 Date of Submission: 15-apr-2022
Grade:
B.3 Conclusion: