Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment No.

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()

Writing the file:


To write some text to a file, we need to open the file using the open method with one of the
following access modes.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of
the file.
a: It will append the existing file. The file pointer is at the end of the file. It creates a new
file if no file exists.
Syntax:
write(“text”)

Reading from file:


To read a file using the Python script, the Python provides the read() method. The read() method
reads a string from the file. It can read the data in the text as well as a binary format.
Syntax:
fileobj.read(<count>)

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.1 Software Code written by student:


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.


B.2 Input and Output:
1) for 1st program:- Open the file in append 'a' mode, and write to
it using write() method. Inside write() method, a string "new
text" is passed. This text is seen on the file as shown above in
output.

2) for 2nd program:- Call open(file, mode) with the pathname of a


file as file and mode as "r" to open the file for reading. Use a
for-loop to iterate through the file. At each iteration, use
str.strip(characters) with "\n" as characters to strip it from
each line str, and use str.split() to create a list containing all the
words from the line str. Also, in each iteration, add 1 to the
number of lines, use len(object) with the list containing the
word from the line as object to add it to the number of words, and
use len(object) with the stripped line as object to add it to the number of
characters.

B.3 Conclusion:

1) for 1st program: In this python program, we learned about files and
their modes, how to write text to the files by solving examples.

2) for 2nd program: In this Python program, we learned how to count


the number of words, lines, Chars in a Text File, with the help of the
above program.

B.4 Question of Curiosity


Q.1. Which of the following is not a method of opening files?
a) Replace b) Append c) Write d) Read
ANS:- a)Replace
Q.2 Which of the following commands can be used to read “n” number of
characters from a file using the file object <file>?
A) file.read(n) B) n = file.read() C) file.readline(n) D)
file.readlines()
ANS:- A) file.read(n)
Q. 3 What is the difference between r+ and w+ mode?
ANS:-

Sr.No Basic points r+ w+

It deletes all the


Opening a It opens the file and places the
content of the file and
File (if file pointer at the beginning of the
1 keeps the point at the
exists) file to read.
beginning of the file.

If the file is not


Creating a
If the file is not present while present while opening
File (if file
2 opening the file in ‘r+’ mode, it the file in ‘w+’ mode, it
does not
throws FileNotFound exception. creates new empty
exist)
file.
As opening file in ‘w+’
You can read the complete file modes deletes the file
3 Reading File
text. content, you can not
read the file.
If you open file in ‘r+’ mode and
It deletes all the old
try to write the content, it starts
Writing File contents from the text
4 writing from the beginning and
Content file and save new text
overwrite the old content with
inside the file.
new.

You might also like