-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie.py
190 lines (156 loc) · 6.21 KB
/
movie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# for os module
import os
# for creating the path file for the project
def get_complete_file_path():
"""complete path of the file."""
# creating foldername
folder_name = 'movieproject'
# creating file_name
file_name = 'moviefile.txt'
# for separator
separator = "/"
# adding folder name, separator and file name
complete_path = folder_name + separator + file_name
# returning the path for the project file
return complete_path
# function for checking if the project file exist or not
def is_project_exists(complete_path):
"""Check project"""
# checking for project file - exist or not using os module
if os.path.exists(complete_path):
return True
# if project file does not exist
return False
# function for displaying all the movies entered
def display_all_the_movies(complete_path):
"""Display all the movies."""
# calling function to verify if project file exist or not
if is_project_exists(complete_path=complete_path):
# opening the file in read mode
with open(complete_path, "r") as file1:
# using enumerate() for displaying movieno
for movieno,mname in enumerate(file1):
print(movieno + 1, ': ', mname)
else:
print("Project does not exists")
# function for checking that entered movie name already exist or not
def is_movie_exists(complete_path, name):
"""Is movie exists or not."""
is_exists = False
# opening file in read mode
file_read = open(complete_path, 'r')
# reading the file
read_movie = file_read.readlines()
# using loop to iterate over the movie file
for movie_name in read_movie:
# checking if the movie name already exist or not
if (movie_name.strip('\n') == name):
print("Movie Name Already Added")
is_exists = True
break
return is_exists
# function to add a movie name to the file
def add_new_movie(complete_path):
"""Adding new Movie"""
# opening th efile in append mode
with open(complete_path, "a") as file2:
# adding the movie name
name = input("Enter the movie name:")
# calling function to see if the movie already exist or not
is_exists = is_movie_exists(complete_path=complete_path, name=name)
# if it does'nt exist then add the movie name in the file
if not is_exists:
# write the movie name in the file
file2.write(name)
file2.write("\n")
# function to delete the movie name from the file
def deleting_movie(complete_path):
"""Deleting A Movie"""
# using if condition -calling function to check if the movie name is already in the file or not
if is_project_exists(complete_path=complete_path):
# get list of lines
# opening file in read mode
a_file = open(complete_path, "r")
# reading the file
lines = a_file.readlines()
# closing the file in read format
a_file.close()
# opening the file in write mode
with open(complete_path, "w") as file3:
del_name = input("Enter the name of the movie you want to delete=")
# searching the movie name to delete
for line in lines:
# if it's found then except that every other movie name is again written
if(line.strip('\n') != del_name):
file3.write(line)
else:
print("Project does not exists")
# function for removing the project
def remove_project(complete_path):
"""Deleting the whole project"""
# using if condition to check if the project exist or not
if is_project_exists(complete_path=complete_path):
# using os to remocve the whole path
os.remove(complete_path)
else:
print("\nProject does not exist")
# function to check if the user want to continue or not
def is_continue():
"""User should continue or not."""
print("\nDo you want to continue: Yes or No=")
# taking input for the user to continue or not
user_choice = input()
return user_choice
#mai n() function
def main():
"""Main function."""
# Do all the things here
"""Creating the file"""
complete_path = get_complete_file_path()
# user_choice is used for loop
user_choice = 'Yes'
# using while loop
while(user_choice.lower() == 'yes'):
# call all other functions
print("Menu\n1. Display All The Movies\n2. Add New Movie\n3. Delete a Movie\n4. Remove The Project\n5. Exit")
menu_choice = int(input("Enter your choice= "))
complete_path = get_complete_file_path()
# for displaying all the entered movies
if (menu_choice == 1):
# for calling the display function
display_all_the_movies(complete_path=complete_path)
# for calling the function to check if user wants to continue or not
user_choice = is_continue()
# for adding a new movie
elif (menu_choice == 2):
# calling function to add a new movie name
add_new_movie(complete_path=complete_path)
# for calling the function to check if user wants to continue or not
user_choice = is_continue()
# for deleting a movie
elif(menu_choice == 3):
# calling function to delete a movie name
deleting_movie(complete_path=complete_path)
# for calling the function to check if user wants to continue or not
user_choice = is_continue()
# for removing the project
elif(menu_choice == 4):
#calling function to remove the project
remove_project(complete_path=complete_path)
# for calling the function to check if user wants to continue or not
user_choice = is_continue()
# for exit
elif(menu_choice == 5):
print("\nThank you")
#to exit the program
exit()
# if wrong choice entered
else:
print("\nWrong Choice Entered")
print("\nDo you want to continue: Yes or No=")
user_choice = input()
else:
print("Thank You")
# main
if __name__ == "__main__":
main()