0% found this document useful (0 votes)
13 views5 pages

6 7

The document contains Python programs to perform file operations like displaying first N lines of a file, finding word frequency in a file, and zipping a folder. It also contains programs to calculate the area of different shapes using OOP concepts and a program to update employee salaries based on department.

Uploaded by

shrinidhi N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

6 7

The document contains Python programs to perform file operations like displaying first N lines of a file, finding word frequency in a file, and zipping a folder. It also contains programs to calculate the area of different shapes using OOP concepts and a program to update employee salaries based on department.

Uploaded by

shrinidhi N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6a.

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

def display_first_N_lines(file_name, N):


try:
with open(file_name, 'r') as file:
for i in range(N):
line = file.readline().strip()
if line:
print(line)
else:
break
except FileNotFoundError:
print("File not found!")

def find_word_frequency(file_name, word):


try:
with open(file_name, 'r') as file:
content = file.read().lower()
word_count = content.count(word.lower())
print(f"The word '{word}' occurs {word_count}
times in the file.")
except FileNotFoundError:
print("File not found!")

def main():
file_name = input("Enter the file path: ")
try:
N = int(input("Enter the number of lines to display:
"))
except ValueError:
print("Invalid input! Please enter a valid number.")
return

display_first_N_lines(file_name, N)
search_word = input("Enter the word to find its
frequency: ")
find_word_frequency(file_name, search_word)

if __name__ == "__main__":
main()
to check output
create a text file named "example.txt" with the following content:

This is an example file.


It contains some lines of text
for demonstrating the Python program.
Let's see how it works!

6b. Write a python program to create a ZIP file of a particular folder which contains
several files inside it.
import os
import zipfile

def zip_folder(folder_path, output_zip_filename):


with zipfile.ZipFile(output_zip_filename, 'w',
zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path,
folder_path)
zipf.write(file_path, arcname)

if __name__ == "__main__":
# Replace this with the path of the folder you want to
zip
folder_to_zip = "C:\Users\DELL\Documents\dummy"
# Replace this with the desired name of the output ZIP
file
output_zip_file = "dummy.zip"

zip_folder(folder_to_zip, output_zip_file)
print(f"Folder '{folder_to_zip}' has been zipped to
'{output_zip_file}'

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 area(self):
pass

class Triangle(Shape):
def __init__(self, base, height):
self.base = base
self.height = height

def area(self):
return 0.5 * self.base * self.height

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return math.pi * self.radius ** 2

class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width

def area(self):
return self.length * self.width

if __name__ == "__main__":
triangle = Triangle(6, 4)
print("Area of Triangle:", triangle.area())

circle = Circle(5)
print("Area of Circle:", circle.area())

rectangle = Rectangle(7, 3)
print("Area of Rectangle:", rectangle.area())

7. b 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, name, employee_id, department,
salary):
self.name = name
self.employee_id = employee_id
self.department = department
self.salary = salary

def update_salary_by_department(self, department,


new_salary):
if self.department == department:
self.salary = new_salary

def __str__(self):
return f"Name: {self.name}, Employee ID:
{self.employee_id}, Department: {self.department}, Salary:
{self.salary}"

if __name__ == "__main__":
employees = [
Employee("A", 1001, "HR", 50000),
Employee("B", 1002, "IT", 60000),
Employee("C", 1003, "HR", 55000),
Employee("D", 1004, "Finance", 70000),
]

for emp in employees:


print(emp)

department_to_update = "HR"
new_salary_for_department = 58000

for emp in employees:

emp.update_salary_by_department(department_to_update,
new_salary_for_department)

print("\nAfter updating salaries:")


for emp in employees:
print(emp)

You might also like