blooming8

libreria python

Dec 31st, 2021
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import os
  2.  
  3. class Libreria():
  4.     def aggiungi(self, fnome):
  5.         while True:
  6.             titolo = input("\nTitolo: ('.' per interrompere)\n").title()
  7.             if titolo == '.':
  8.                 break
  9.             autore = input("\nAutore: \n").title()
  10.             ''' Scrittura sul file '''
  11.             with open(fnome, "a") as file:
  12.                 file.write(f"{titolo} - {autore}\n")
  13.                 print(f"\nHai appena aggiunto {titolo} di {autore}.\n")
  14.         return 0
  15.          
  16.     def rimuovi(self, fnome):
  17.         ''' Chiediamo all'utente il numero della riga da cancellare, ma si tratta di un
  18.        indice falsato (la conta delle righe inizia da 0). '''
  19.         i = int(input("\nIndice da rimuovere:\n"))
  20.         ''' Sfruttiamo il metodo readlines() per raggruppare il contenuto
  21.        del file in una lista. I metodi delle liste vengono in nostro soccorso. '''
  22.         with open(fnome, "r+") as file:
  23.             righe = file.readlines()
  24.             ''' Decrementiamo l'indice inserito dall'utente per la ragione vista sopra. '''
  25.             i -= 1
  26.             for riga in righe:
  27.                 if i == righe.index(riga):
  28.                     print(f"\nHai cancellato {riga}")
  29.                     righe.pop(i)
  30.         ''' Stampiamo file.readlines() sul file. '''
  31.         ''' Riaprire il file in modalità 'w' ci consente di resettarlo. '''    
  32.         with open(fnome, "w") as file:
  33.             file.writelines(righe)
  34.         return 0
  35.  
  36.     def mostra(self, fnome):
  37.         ''' Contatore di righe '''
  38.         i = 0
  39.         ''' Lettura del file '''
  40.         with open(fnome, "r") as file:
  41.             print("\n")
  42.             for riga in file:
  43.                 ''' anche qui l'indice è falsato; lo si incrementa '''
  44.                 i += 1
  45.                 print(f"[{i}] {riga}")
  46.         return 0
  47.  
  48.     def canc_file(self, fnome):
  49.         s_n = input("\nSicuro/a di voler cancellare il file [s/n]?\n")
  50.         if s_n == 's':    
  51.             os.remove(fnome)
  52.             return 0
  53.         elif s_n == 'n':
  54.             return 1
  55.         else:
  56.             print("\nScelta non valida.\n")
  57.             return 1
  58.        
  59.     def cerca(self, fnome):
  60.         titolo = input("\nTitolo da cercare:")
  61.         if titolo in fnome:
  62.             return 1
  63.         else:
  64.             return 0
  65.  
  66. def main():
  67.     libr = Libreria()
  68.     fnome = "libri.txt"
  69.    
  70.     if not os.path.exists(fnome):
  71.         print("\nFile non trovato.\n")
  72.            
  73.     while True:
  74.         try:
  75.             scelta = int(input("\nLIBRERIA DI TESTO\n\n1. Aggiungi Libro\n2. Rimuovi Libro\n3. Mostra Libri\n4. Cancella File\n5. Cerca\n6. Esci\n"))
  76.             if scelta == 1:
  77.                 libr.aggiungi(fnome)
  78.             elif scelta == 2:
  79.                 libr.rimuovi(fnome)
  80.             elif scelta == 3:
  81.                 libr.mostra(fnome)
  82.             elif scelta == 4:
  83.                 libr.canc_file(fnome)
  84.             elif scelta == 5:
  85.                 libr.cerca(fnome)
  86.             elif scelta == 6:
  87.                 print("\nArrivederci!")
  88.                 break
  89.             else:
  90.                 print("\nDevi mettere un numero tra 1 e 5.")
  91.         except ValueError:
  92.             print("\nNon puoi inserire lettere.")
  93.  
  94. if __name__ == '__main__':
  95.     main()
  96.  
Advertisement
Add Comment
Please, Sign In to add comment