Zaid File Handing
Zaid File Handing
Python Code:
import pickle
import os
def initialize_movie_file(file_name):
"""Create the binary file with sample movie data if it doesn't exist."""
if not os.path.exists(file_name):
sample_movies = [
pickle.dump(sample_movies, f)
pickle.dump(movie_list, f)
def load_from_binary(file_name):
if not os.path.exists(file_name):
print(f"File '{file_name}' not found. Starting with an empty collection.")
return []
try:
return pickle.load(f)
except EOFError:
return []
def show_movies(movies):
if not movies:
else:
if movie['title'].lower() == movie_title.lower():
return
movies[pos] = new_info
print(f"Movie updated to: {new_info['title']} (Director: {new_info['director']}, Year: {new_info['year']})")
else:
removed_movie = movies.pop(pos)
else:
if not movies:
return movies
return sorted_movies
def count_movies(movies):
confirm = input("Are you sure you want to clear the entire collection? (yes/no): ").strip().lower()
if confirm == 'yes':
movies.clear()
save_to_binary(file_name, movies)
else:
print("Operation canceled.")
try:
shutil.copy(file_name, backup_file)
except FileNotFoundError:
try:
shutil.copy(backup_file, file_name)
except FileNotFoundError:
def movie_menu():
file_name = 'movie_collection.bin'
movies = load_from_binary(file_name)
while True:
print("\nMovie Collection Menu:")
print("11. Exit")
if choice == '1':
save_to_binary(file_name, movies)
show_movies(movies)
show_movies(movies)
try:
remove_movie(movies, pos)
save_to_binary(file_name, movies)
except ValueError:
print("Invalid input.")
find_movie(movies, title)
show_movies(movies)
try:
save_to_binary(file_name, movies)
except ValueError:
print("Invalid input.")
show_movies(movies)
save_to_binary(file_name, movies)
else:
count_movies(movies)
clear_collection(movies, file_name)
backup_collection(file_name, backup_file)
elif choice == '10':
restore_collection(file_name, backup_file)
break
else:
print("Invalid choice.")
if __name__ == "__main__":
movie_menu()
Output:
# Menu-Driven Program for List Operations on a Binary File (Student
Data)
Python Code:
import pickle
import os
def initialize_student_file(file_name):
"""Create the binary file with sample student data if it doesn't exist."""
if not os.path.exists(file_name):
sample_students = {
pickle.dump(sample_students, f)
pickle.dump(students, f)
def load_students(file_name):
if not os.path.exists(file_name):
return {}
try:
with open(file_name, 'rb') as f:
return pickle.load(f)
except EOFError:
return {}
def show_students(students):
if not students:
else:
if student_id in students:
del students[student_id]
else:
def student_menu():
file_name = 'students.bin'
initialize_student_file(file_name)
students = load_students(file_name)
while True:
print("4. Exit")
if choice == '1':
save_students(file_name, students)
show_students(students)
delete_student(students, student_id)
save_students(file_name, students)
break
else:
if __name__ == "__main__":
student_menu()
Output:
C.S.V File Programs:
Python Code:
import csv
import os
def initialize_csv_file(filename):
"""Initialize the CSV file with sample book data if it doesn't exist."""
if not os.path.exists(filename):
writer = csv.writer(file)
def display_books(filename):
if not os.path.exists(filename):
return
reader = csv.reader(file)
print("\nLibrary Collection:")
print(", ".join(row))
def add_book(filename):
writer = csv.writer(file)
def search_book(filename):
found = False
if not os.path.exists(filename):
return
reader = csv.reader(file)
if row[0] == book_id_to_search:
found = True
break
if not found:
def library_menu():
filename = 'library.csv'
initialize_csv_file(filename)
while True:
print("4. Exit")
if choice == '1':
display_books(filename)
add_book(filename)
search_book(filename)
break
else:
if __name__ == "__main__":
library_menu()
Output: