Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- class Film():
- titolo, anno = None, None
- @classmethod
- def get_info(cls):
- if cls.anno is not None:
- return f"{cls.titolo} ({cls.anno})"
- else:
- return f"{cls.titolo}"
- @classmethod
- def aggiungi(cls, file):
- cls.titolo, cls.anno = None, None
- if not os.path.exists(file):
- with open(file, "w") as f:
- f.write("--- LISTA FILM ---")
- print("\nIl file 'movies.txt' non esisteva ed è quindi stato creato.")
- cls.titolo = input("\nTitolo?\n").title()
- with open(file, "r") as f:
- if f.read().find(cls.titolo) != (-1):
- print("\nFilm già inserito.\n")
- return 1
- else:
- tmp = input("\nAnno? (schiaccia '@' se non c'è l'anno)\n")
- if tmp != "@":
- cls.anno = tmp
- with open(file, "a") as f:
- f.write(f"\n{Film.get_info()}")
- '''
- righe = f.readlines()
- with open(file, "w") as f:
- f.writelines(righe.sort())
- '''
- return 0
- @classmethod
- def rimuovi(cls, file):
- i = int(input("\nIndice da rimuovere?\n"))
- with open(file, "r+") as f:
- i -= 1
- for riga in f.readlines():
- if not riga.startswith("-"):
- if i == f.readlines().index(riga):
- f.readlines().pop(i)
- with open(file, "w") as f:
- f.writelines(f.readlines().sort())
- return 0
- @classmethod
- def mostra(file):
- i = 0
- with open(file, "r") as f:
- for riga in f:
- if riga.startswith("-"):
- print(f"{riga}")
- else:
- i += 1
- print(f"\n{i}) {riga}")
- return 0
- def cancfile(file):
- os.remove(file)
- return 0
- def main():
- file = "movies.txt"
- while 1:
- menu = int(input("\nmenu...\n"))
- if menu == 1:
- if not Film.aggiungi(file):
- print(f"\nHai inserito: {Film.get_info()}.")
- else:
- print("\nInserimento annullato.\n")
- elif menu == 2:
- Film.rimuovi(file)
- elif menu == 3:
- Film.mostra(file)
- elif menu == 4:
- if not cancfile(file):
- print("\nFile cancellato.\n")
- else:
- print("\nErrore nella rimozione.\n")
- elif menu == 5:
- break
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment