impressive_i

Search books by ISBN books in a text file

Oct 16th, 2019
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Search books by ISBN books in a text file
  2. # Attention! file books.txt should lie next to the script .py
  3. # created by impressive_i
  4.  
  5. #Метод поиска книги
  6. def searchByISBN( isbn ):
  7.     isbn += " "
  8.     count = 0
  9.     fileBooks.seek(0)
  10.     for line in fileBooks:
  11.         count += 1    
  12.         line = line.rstrip()
  13.         if isbn in line:
  14.             startIndex = line.find(' ') + 1
  15.             return "Книга #" + str(count) + " " + line[startIndex:]            
  16.     return "Данной книги нет в списке"
  17.  
  18. # Путь до файла с книгами
  19. pathbooks = "Books.txt"
  20.  
  21. try:
  22.     fileBooks = open(pathbooks, "r")
  23.     try:
  24.         symbolsForOutput = ("exit", 0, "0", False, "out")
  25.         while(True):
  26.             isbn = input("Put ISBN or \"exit\": ")
  27.             if isbn in symbolsForOutput:
  28.                 break
  29.             currentbook = searchByISBN( isbn )
  30.             print( currentbook )
  31.        
  32.     except Exception as e:
  33.         print("Error: ошибка чтения файла: ", e)
  34.     finally:
  35.         fileBooks.close()
  36.        
  37. except Exception as ex:
  38.     print("Error: ошибка при открытии файла: ", ex)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×