You are on page 1of 6

Department of Computing

CS114: Fundamentals of Programming


Class: BESE 10 AB

Lab 11: File Handling & Built In Functions In Python


CLO4: Adopt the latest IDEs and other supplementary tools to aid code
implementation and management

Instructor: Ms. Hania Aslam

Date: December 6th , 2019


Time: 9:00am -12:00pm and 02:00pm -05:00pm

CS114: Fundamentals of Programming Page 1


Lab 11: File Handling & Built In Functions 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"

file = open(filename, "r")

Once a file has been opened. Different read and write operations can be performed on it by using
the following built-in functions:

read() #return one big string

readline() #return one line at a time

readlines() #returns a list of lines

write () #Used to write a fixed sequence of characters to a file

writelines() #writelines can write a list of strings.

When you’re done working with a file, use close() to close it by using the following syntax:

file.close()

CS114: Fundamentals of Programming Page 2


Lab Tasks:

Perform the following tasks:

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:

Automatic number-plate recognition (ANPR) is a technology that uses optical


character recognition on images to read vehicle registration plates to create vehicle
location data. It can use existing closed-circuit television, road-rule enforcement
cameras, or cameras specifically designed for the task. ANPR is used by police forces
around the world for law enforcement purposes, including to check if a vehicle is
registered or licensed. It is also used for electronic toll collection on pay-per-use roads
and as a method of cataloguing the movements of traffic, for example by highways
agencies.

[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]

CS114: Fundamentals of Programming Page 3


Task 2(b)
#Add the snaps of your Python code. [1 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]

Task 5: A palindrome is a string which is same either read forward or backwards.


Example: String “radar” is a palindrome as after reversal it remains same!

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

CS114: Fundamentals of Programming Page 4


mentioned in your slides to convert it to a string OR alternatively,you can use string slicing to
achieve your objective here.

Help resource: String slicing , indexing and striding

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:

Password Strength Calculation 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

CS114: Fundamentals of Programming Page 5


Deliverables
Compile a single Word document by filling in the solution/answer part (as directed) along with
the snapshots. Name your submission file as given below and submit this Word file on LMS
before the deadline.

Name – Registration No. – Section

Grade Criteria
This lab is graded. Min marks: 0. Max marks: 10.

Activity Minimum Maximum


Documentation with clearly defined Fail Pass
understanding of the lab task and approach
Task 1 0 2
Task 2 0 4
Task 3 0 2
Task 4 0 2
Task 5 0 5
Task 6 0 5

CS114: Fundamentals of Programming Page 6

You might also like