1. Create a python program named One.
py and copy the lines that
are starting with 'A' in another file named "[Link]"
def copy_lines_starting_with_a(source_file, destination_file):
try:
with open(source_file, 'r') as src, open(destination_file, 'w') as dest:
for line in src:
if [Link]('A'):
[Link](line)
print(f"Lines starting with 'A' have been copied to
{destination_file}.")
except FileNotFoundError:
print(f"The file {source_file} was not found.")
except Exception as e:
print(f"An error occurred: {e}")
# Specify source and destination files
source_file = '[Link]'
destination_file = '[Link]'
copy_lines_starting_with_a(source_file, destination_file)
2. Write a python program using list to implement stack data structure
and perform the following operations: a. Push(), b. Pop(), c. Display()
d. Peek()
max_length = int(input("Enter the length of the stack: "))
stack = []
def is_empty():
if len(stack)==0:
return True
else:
return False
def push(item):
if len(stack) == max_length:
print("Stack overflow")
else:
[Link](item)
print(f"Item '{item}' successfully pushed.")
def pop():
if is_empty():
print("Stack is empty, underflow")
else:
item = [Link]()
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")
choice = int(input("Enter your choice: "))
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
"[Link]".
i) Addbin() -to write a dictionary that holds students roll no and mark
as key value pairs into a binary file "[Link]".
ii) Dispbin() to display the roll numbers and marks of students
who secured more than 70 marks
import pickle
def addbin():
try:
with open("[Link]", '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
[Link](student_data, file)
print("records appended")
except Exception as e:
print("An error occurred: ",e)
def dispbin():
try:
with open("[Link]", 'rb') as file:
while True:
try:
student_data = [Link](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.")
[Link] a python program named "[Link]" to store the details of
Empno,Name and Salary in "[Link]" 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("[Link]",'w', newline='') as file:
writer = [Link](file)
[Link](["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:")
[Link]([empno, name, salary])
print("[Link] 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("[Link]",'r') as file:
reader = [Link](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 [Link] 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 [Link] to read a text file
[Link] and display the number of vowels/consonants/lower case/
upper case characters.
def count_characters():
try:
with open("[Link]",'r') as file:
text = [Link]()
vowels = "aeiouAEIOU"
vc = cc = lc = uc =0
for char in text:
if [Link]():
if char in vowels:
vc += 1
else:
cc += 1
if [Link]():
lc += 1
elif [Link]():
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()