NOTE: I Hope the
below solution meets your requirements as per the question. Please
Do UPVOTE if it Does. :)
Instead performing the operations in the console its
better to perform it in the file. So the program has been modified
for dynamic operations, you can ask the user for different file
names and text to search for in the quotes file.
## Code for the
required function:
# Function to check the text in the quotes
def quote(file_name, text):
# Handling file not found error using try block
try:
# opening the quotes file in read mode
file = open(file_name, 'r')
# creating an object for the write file
output_file = open('watchQuotes.txt','w')
# iterating through each quote of the file
for quote in file.readlines():
# spliting the quote based on spaces
line = quote.split()
# checking each word of the line with the text
for word in line:
# if word matches the text, printing the quote to console and
writing the quote to file
if word.strip(',') == text:
quote = ' '.join(line)+'\n'
output_file.write(quote)
print(quote,end='')
# printing the error message if file not found
except FileNotFoundError:
print('file',file_name,'not found.Exiting program')
exit()
# Asking the user for the file name with quotes
file_name = input('Enter the File Name which contains quotes:
')
text = input('Enter the text to found in the file with quotes :
')
quote(file_name,text)
## Screenshot of
the code in IDLE:
![text in quotes.py- C/Users/brije/Desktop/Chegg Problems/text_in_quotes.py (3.6.8) Eile Edit Format Bun Options Window Help Fu](//img.homeworklib.com/questions/1f5701a0-f43c-11eb-baef-855c88d4799e.png?x-oss-process=image/resize,w_560)
## Screenshot of
the output:
![Python 3.6.8 Shell File Edit Shell Debug Options Window Help Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MS](//img.homeworklib.com/questions/1fd3e1e0-f43c-11eb-8ff9-9fa5944d19b5.png?x-oss-process=image/resize,w_560)
## Screenshot of
the Input file and Output File with Quotes:
![watchQuotes.bt - Notepad File Edit Format View Help The Price of freedom is eternal vigilance. Thomas Jefferson Anyone who co](//img.homeworklib.com/questions/2048e3e0-f43c-11eb-bc3f-e78ebb5fd219.png?x-oss-process=image/resize,w_560)
text in quotes.py- C/Users/brije/Desktop/Chegg Problems/text_in_quotes.py (3.6.8) Eile Edit Format Bun Options Window Help Function to check the text in the quotes der q text): file n found error using try block try: opening the quotea file in read mode open (file name, file write file open ('watchQuotes . txt', 'w' ) file output iterating through each quote of the file for quote in file.readlines () ed on apacea line quote.split checking each word of the line with the text for word in 1ine: tches the text, printing the quote to console and writing the quote to file if word, strip(.' ) == text: quote ..join (line)+'\n output_rile.write (quote) # printing the error message if file not found except FileNot FoundError: file',file name, 'not found.Exiting program) exit Asking the user for the file name with quotea file name pu E e Elle Name ontans quotes: found in tl file with quotes: input quote (file name, text) Ln: 26 Col: 64 0827 PM F ENG 13-06-2019 O
Python 3.6.8 Shell File Edit Shell Debug Options Window Help Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v. 1916 64 bit (AMD64) on win32 Type "help", "copyright", "credits" or "license ()" for more 1nformation. RESTART C:/Users/brije/Desktop/Chegg Problems/text in quotes.py == - Enter the File Name which contains quotes: quote.txt | Enter tne texe o rouna Iile with quotes 13 found.Exiting program quote.txt RESTART: C:/Users/brije/Desktop/Chegg Problems/text in_quotes.py===== Enter the File Name which contains quotes: quotes.txt The Price of freedom is eternal vigilance. Thomas Jefferson 'Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.' John Von Neumann And so it often happens that an apparently ingenious idea is in fact a weakness which the acientific cryptographer seizea on for his solution. Herbert Yardley In: 15 Col 4 08-25 PM F ENG 13-06-2019 O
watchQuotes.bt - Notepad File Edit Format View Help The Price of freedom is eternal vigilance. Thomas Jefferson Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin. John Von Neumann 'And so it often happens that an apparently ingenious idea is in fact a weakness which the scientific cryptographer seizes on for his solution Herbert Yardley quotes.tt Notepad X File Edit Format View Help 'The Price of freedom is eternal vigilance.' Thomas Jefferson Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.' John Von Neumann And so it often happens that an apparently ingenious idea is in fact a weakness which the scientific cryptographer seizes on for his solution. Herbert Yardley 0831 PM F 4ENG 13-06-2019 O