Advertisement
_bzium

Untitled

Mar 21st, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. filename = input("Give me a file name: ")
  2. phrase = input("What phrase you want to find: ")
  3.  
  4. def phrase_lookup(filename, phrase):
  5.     try:
  6.         with open(filename, "r") as f:
  7.             for line_num, line in enumerate(f, 1):
  8.                 if phrase in line:
  9.                     bold_phrase = line.replace(phrase, f"\033[1m{phrase}\033[0m")
  10.                     print(f"Found '{phrase}' in line {line_num}: {bold_phrase.strip()}")
  11.     except FileNotFoundError:
  12.         print(f"The file {filename} does not exists.")
  13.  
  14. if __name__=="__main__":
  15.     phrase_lookup(filename, phrase)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement