Question

Write a function quote() that takes a file as an input file (quotes.txt), searches it looking...

  1. Write a function quote() that takes a file as an input file (quotes.txt), searches it looking for the piece of text (i.e., a string) also given as input, and saves quotes containing that text to a file called watchQuotes.txt one quote per line and print to the screen.

>>> quote('quote.txt', 'is')

file quote.txt not found. Exiting program.

>>> quote('quotes.txt', 'is')

>>> '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

>>>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

## 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

## 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

Add a comment
Know the answer?
Add Answer to:
Write a function quote() that takes a file as an input file (quotes.txt), searches it looking...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT