0% found this document useful (0 votes)
21 views

Python 6 7

The document contains two Python programs. The first program (6a) accepts a file name from the user, displays the first N lines of the file, and finds the frequency of a word entered by the user. The second program (6b) creates a ZIP file from a user-specified folder containing multiple files. The document also contains two additional Python programs. The first (7a) uses inheritance to calculate the area of shapes like circles, rectangles, and triangles. The second (7b) creates an Employee class to store employee details like name, ID, department, and salary, and includes a method to update salaries for employees in a given department.

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python 6 7

The document contains two Python programs. The first program (6a) accepts a file name from the user, displays the first N lines of the file, and finds the frequency of a word entered by the user. The second program (6b) creates a ZIP file from a user-specified folder containing multiple files. The document also contains two additional Python programs. The first (7a) uses inheritance to calculate the area of shapes like circles, rectangles, and triangles. The second (7b) creates an Employee class to store employee details like name, ID, department, and salary, and includes a method to update salaries for employees in a given department.

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6.

a) a) Write a python program to accept a file name from the user


and perform the following operations
1. Display the first N line of the file
2. Find the frequency of occurrence of the word accepted from the
user in the file
import os.path

import sys

fname = input("Enter the filename : ")

if not os.path.isfile(fname):

print("File", fname, "doesn't exists")

sys.exit(0)

infile = open(fname, "r")

lineList = infile.readlines()

#print(lineList)

for i in range(len(lineList)):

print(lineList[i])

word = input("Enter a word : ")

cnt = 0

for line in lineList:

cnt += line.count(word)

print("The word", word, "appears", cnt, "times in the file")

6.b) Write a python program to create a ZIP file of a particular folder


which contains several files inside it.

import os

import sys
import pathlib

import zipfile

dirName = input("Enter Directory name that you want to backup : ")

if not os.path.isdir(dirName):

print("Directory", dirName, "doesn't exists")

sys.exit(0)

curDirectory = pathlib.Path(dirName)

with zipfile.ZipFile("myZip.zip", mode="w") as archive:

for file_path in curDirectory.rglob("*"):

archive.write(file_path,
arcname=file_path.relative_to(curDirectory))

if os.path.isfile("myZip.zip"):

print("Archive", "myZip.zip", "created successfully")

else:

print("Error in creating zip archive")

7.a) By using the concept of inheritance write a python program to


find the area of triangle, circle and rectangle.
import math

class Shape:

def __init__(self):

self.area = 0

self.name = ""

def showArea(self):

print("The area of the", self.name, "is", self.area, "units")


class Circle(Shape):

def __init__(self,radius):

self.area = 0

self.name = "Circle"

self.radius = radius

def calcArea(self):

self.area = math.pi * self.radius * self.radius

class Rectangle(Shape):

def __init__(self,length,breadth):

self.area = 0

self.name = "Rectangle"

self.length = length

self.breadth = breadth

def calcArea(self):

self.area = self.length * self.breadth

class Triangle(Shape):

def __init__(self,base,height):

self.area = 0

self.name = "Triangle"

self.base = base

self.height = height

def calcArea(self):

self.area = self.base * self.height / 2


c1 = Circle(5)

c1.calcArea()

c1.showArea()

r1 = Rectangle(5, 4)

r1.calcArea()

r1.showArea()

t1 = Triangle(3, 4)

t1.calcArea()

t1.showArea()

7b. Write a python program by creating a class called Employee to


store the details of Name, Employee ID, Department and Salary, and
implement a method to update salary of employees belonging to a given
department.

class Employee:

def _init_(self):

self.name = ""

self.empId = ""

self.dept = ""

self.oldsalary = 0

def getEmpDetails(self):

self.name = input("Enter Employee name : ")

self.empId = input("Enter Employee ID : ")

self.dept = input("Enter Employee Dept : ")

self.oldsalary = int(input("Enter Employee Salary : "))


def showEmpDetails(self):

print("Employee Details")

print("Name : ", self.name)

print("ID : ", self.empId)

print("Dept : ", self.dept)

print("Salary : ", self.oldsalary)

def updtSalary(self):

self.newsalary = int(input("Enter new Salary : "))

self.a=self.newsalary+self.oldsalary

print("Updated Salary", self.a)

e1 = Employee()

e1.getEmpDetails()

e1.showEmpDetails()

e1.updtSalary()

You might also like