Lab - 11 - File Handling in Python
Lab - 11 - File Handling in Python
Lab - 11 - File Handling in Python
Introduction:
The term file management in the context of computers refers to the manipulation of data in a file
or files and documents on a computer. To manipulate files in Python we can use the built-in
object "file". That object provides basic functions and methods necessary to manipulate files by
default. In this lab you will learn to use the different methods of the file object.
Objectives:
The purpose of this lab is to get familiar with basic functions and file handling operations in
Python.
Tools/Software Requirement:
Python IDLE
Description:
The open() function is used to open files in our system, the filename is the name of the file to be
opened. The mode indicates, how the file is going to be opened "r" for reading, "w" for writing
and "a" for a appending.
filename = "Text.txt"
Once a file has been opened. Different read and write operations can be performed on it by using
the following built-in functions:
When you’re done working with a file, use close() to close it by using the following syntax:
file.close()
Task 1:
The following paragraph has been taken from an internet source. Store this in a file and write a
program that programmatically reads and displays number of lines and words present in the
paragraph:
[2.0 Marks]
Task 1
#Add the snaps of your Python code. [1.5 Mark]
#Add the snaps of your code execution. [0.5 Mark]
Task 2:
(a) Write a function that accepts a file name and a search term from the user. Your function
should check the existence of the search term in the file and return appropriate results. Test the
working of your function against a text file with arguments of your choice.
[3.0
Marks]
Task 2(a)
#Add the snaps of your Python code. [2Marks]
#Add the snaps of your code execution. [1 Mark]
(b) Extend your program by determining and printing the frequency of the user provided search
term inside the test file. [1.0 Mark]
Task 3:
Write a Python function that creates a text file and writes 100 random numbers into it. Next write
another python function which reads the same text file, and prints out the smallest and the largest
numbers.
Task 3
#Add your Python Script code here. [1.5 Marks]
#Add the snap of tasks execution here. [0.5 Mark]
[2.0 Marks]
Task 4:
Write a function, sumColumn(filename), that should read a file that has exactly one integer on
each line and returns the sum of these integers. Apply necessary checks on the file data to get
accurate results.
Task 4
#Add the snaps of your Python code. [1.5 Marks]
#Add the snaps of your code execution. [0.5 Mark]
[2.0 Marks]
Write a python function that checks if string received as an argument, is palindrome or not, and
returns the result to the caller.
Task 5
#Add your Python Script code here. [4 Marks]
#Add the snap of tasks execution here. [1 Mark]
[5.0 Marks]
Hint: There is a built in method to reverse a string however it’s returned results are in the form
of an iterator object, you may traverse it and use it in combination with another function
Task 6: To secure our data, we must create harder to guess passwords with a mix of both upper and
lower case characters along with numbers. Write a modular program that evaluates strength of user
entered password and stores it into a file only if password strength exceeds 8. The program
should return a score to tell the end-user how secure their password is based on the following
criteria:
Criteria Score
The password is at least 8 characters long and less than 16 characters. +2 pts
The password contains both numbers and letters. +3 pts
The password contains at least one punctuation sign from the following three: [$@#] +3 pts
The password contains at least 1 lowercase and 1 uppercase character. +2 pts
[5.0 Marks]
Task 6
#Add the snaps of your Python code. [4.0 Marks]
#Add the snaps of your code execution. [1 Mark]
Hint: You may want to use Python’s built in Module re to create this program. Please consult the
following links :
https://www.tutorialspoint.com/python/python_reg_expressions.htm
https://www.w3schools.com/python/python_regex.asp
Grade Criteria
This lab is graded. Min marks: 0. Max marks: 10.