blooming8

Untitled

Jan 20th, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. import os
  2.  
  3. class Film():
  4.     titolo, anno = None, None
  5.    
  6.     @classmethod
  7.     def get_info(cls):
  8.         if cls.anno is not None:
  9.             return f"{cls.titolo} ({cls.anno})"
  10.         else:
  11.             return f"{cls.titolo}"
  12.  
  13.     @classmethod
  14.     def aggiungi(cls, file):
  15.         cls.titolo, cls.anno = None, None
  16.         if not os.path.exists(file):
  17.             with open(file, "w") as f:
  18.                 f.write("--- LISTA FILM ---")
  19.             print("\nIl file 'movies.txt' non esisteva ed è quindi stato creato.")
  20.         cls.titolo = input("\nTitolo?\n").title()
  21.         with open(file, "r") as f:
  22.             if f.read().find(cls.titolo) != (-1):
  23.                 print("\nFilm già inserito.\n")
  24.                 return 1
  25.             else:
  26.                 tmp = input("\nAnno? (schiaccia '@' se non c'è l'anno)\n")
  27.                 if tmp != "@":
  28.                     cls.anno = tmp
  29.                 with open(file, "a") as f:
  30.                     f.write(f"\n{Film.get_info()}")
  31.                 '''
  32.                righe = f.readlines()
  33.                with open(file, "w") as f:
  34.                    f.writelines(righe.sort())
  35.                '''
  36.         return 0
  37.  
  38.     @classmethod
  39.     def rimuovi(cls, file):
  40.         i = int(input("\nIndice da rimuovere?\n"))
  41.         with open(file, "r+") as f:
  42.             i -= 1
  43.             for riga in f.readlines():
  44.                 if not riga.startswith("-"):
  45.                     if i == f.readlines().index(riga):
  46.                         f.readlines().pop(i)
  47.         with open(file, "w") as f:
  48.             f.writelines(f.readlines().sort())
  49.         return 0
  50.            
  51.     @classmethod
  52.     def mostra(file):
  53.         i = 0
  54.         with open(file, "r") as f:
  55.             for riga in f:
  56.                 if riga.startswith("-"):
  57.                     print(f"{riga}")
  58.                 else:
  59.                     i += 1
  60.                     print(f"\n{i}) {riga}")
  61.         return 0
  62.    
  63.     def cancfile(file):
  64.         os.remove(file)
  65.         return 0
  66.    
  67. def main():
  68.     file = "movies.txt"
  69.  
  70.    
  71.     while 1:
  72.         menu = int(input("\nmenu...\n"))
  73.         if menu == 1:
  74.             if not Film.aggiungi(file):
  75.                 print(f"\nHai inserito: {Film.get_info()}.")
  76.             else:
  77.                 print("\nInserimento annullato.\n")
  78.         elif menu == 2:
  79.             Film.rimuovi(file)
  80.         elif menu == 3:
  81.             Film.mostra(file)
  82.         elif menu == 4:
  83.             if not cancfile(file):
  84.                 print("\nFile cancellato.\n")
  85.             else:
  86.                 print("\nErrore nella rimozione.\n")
  87.         elif menu == 5:
  88.             break
  89.    
  90.    
  91.    
  92.    
  93.  
  94. if __name__ == '__main__':
  95.     main()
  96.  
Advertisement
Add Comment
Please, Sign In to add comment