Practical New
Practical New
copy_lines_starting_with_a(source_file, destination_file)
def push(item):
if len(stack) == max_length:
print("Stack overflow")
else:
stack.append(item)
print(f"Item '{item}' successfully pushed.")
def pop():
if is_empty():
print("Stack is empty, underflow")
else:
item = stack.pop()
print(f"Stack element '{item}' removed.")
def display():
if is_empty():
print("Stack is empty.")
else:
print("Stack contents:", stack)
def peek():
if is_empty():
print("Stack is empty, no top element.")
else:
print(f"Top element is: {stack[-1]}")
while True:
print("\nMenu:")
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Peek")
print("5. Exit")
if choice == 1:
item = input("Enter the item to push: ")
push(item)
elif choice == 2:
pop()
elif choice == 3:
display()
elif choice == 4:
peek()
elif choice == 5:
print("Exiting program.")
break
else:
print("Invalid choice, please try again.")
3. Implement two functions in python to create and access binary file
"student.bin".
i) Addbin() -to write a dictionary that holds students roll no and mark
as key value pairs into a binary file "student.bin".
ii) Dispbin() to display the roll numbers and marks of students
who secured more than 70 marks
import pickle
def addbin():
try:
with open("student.bin", 'ab') as file:
num_students = int(input("Enter the number of students: "))
student_data = {}
for i in range(num_students):
roll_no = input("Enter student roll number: ")
marks = int(input("Enter student marks: "))
student_data[roll_no] = marks
pickle.dump(student_data, file)
print("records appended")
except Exception as e:
print("An error occurred: ",e)
def dispbin():
try:
with open("student.bin", 'rb') as file:
while True:
try:
student_data = pickle.load(file)
except:
break
print("Students with marks greater than 70:")
for roll_no in student_data:
if student_data[roll_no]>70:
print("student roll no:",roll_no," Mark :",student_data[roll_no])
except Exception as e:
print("An error occurred: ",e)
while True:
print("\nChoose an operation:")
print("1. Add student records to binary file")
print("2. Display students with marks > 70")
print("3. Exit")
choice = input("Enter your choice (1-3): ")
if choice == '1':
addbin()
elif choice == '2':
dispbin()
elif choice == '3':
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
4.Creating a python program named "Mycsv.py" to store the details of
Empno,Name and Salary in "Emp.csv" and search for an Empno
entered by the user. If found, display the details of the Employee else
display Empno is not present.
import csv
def create_emp_csv():
try:
with open("Emp.csv",'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Empno", "Name", "Salary"])
num_employees = int(input("Enter the number of employees: "))
for _ in range(num_employees):
empno = input("\nEnter Employee Number: ")
name = input("Enter Employee Name: ")
salary = input("Enter Employee Salary:")
writer.writerow([empno, name, salary])
print("emp.csv created successfully with employee details.")
except Exception as e:
print("An error occurred: ",e)
def search_emp_csv():
try:
empno_to_search = input("Enter the Employee Number to search: ")
found = False
with open("Emp.csv",'r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == empno_to_search:
print("Employee Found:")
print("Empno:",row[0],"Name:",row[1],"Salary: ",row[2])
found = True
break
if not found:
print("employee not found")
except FileNotFoundError:
print(f"The file Emp.csv does not exist.")
except Exception as e:
print("An error occurred: ",e)
while True:
print("\nChoose an operation:")
print("1. Create Employee CSV")
print("2. Search Employee by Empno")
print("3. Exit")
choice = input("Enter your choice (1-3): ")
if choice == '1':
create_emp_csv()
elif choice == '2':
search_emp_csv()
elif choice == '3':
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
5. Create a python program named Count.py to read a text file
Mytext.txt and display the number of vowels/consonants/lower case/
upper case characters.
def count_characters():
try:
with open("myfile.txt",'r') as file:
text = file.read()
vowels = "aeiouAEIOU"
vc = cc = lc = uc =0
for char in text:
if char.isalpha():
if char in vowels:
vc += 1
else:
cc += 1
if char.islower():
lc += 1
elif char.isupper():
uc += 1
print(f"Number of vowels: {vc}")
print(f"Number of consonants: {cc}")
print(f"Number of lowercase characters: {lc}")
print(f"Number of uppercase characters: {uc}")
except FileNotFoundError:
print("The file does not exist.")
except Exception as e:
print("An error occurred:",e)
count_characters()