Question: Python Modify the quote generator program (on this page) such that if there are multiple quotes for the given author, instead of writing them out

Python

Modify the quote generator program (on this page) such that if there are multiple quotes for the given author, instead of writing them out into the same file, the program instead creates a new file for each quote, hence if you have 3 quotes for an author, you would have 3 files in that author's folder, named quote_1.txt, quote_2.txt, quote_3.txt. If the author has only 1 quote, then file can be named wither quote.txt or quote_1.txt

Make change in below program

import json, os import shutil PARENT_DIR = "quotes_output" with open("quotes.json", 'r') as quotes_file: data = json.load(quotes_file) #if the parent directory already there, we will delete it if os.path.exists(PARENT_DIR): shutil.rmtree(PARENT_DIR)#os.rmdir(PARENT_DIR) os.mkdir(PARENT_DIR) #parent directory os.chdir(PARENT_DIR) #change directory so that we are inside the parent directory print("Created parent directory ", PARENT_DIR) for node in data: corrected_author = node["author"] if node["author"] is not None else "Unknown" dir_name = corrected_author.replace(" ", "_") os.mkdir(dir_name) if not os.path.exists(dir_name) else print("{} already exists".format(dir_name)) os.chdir(dir_name) # go inside the newly created directory out = open("quote.txt", "a") out.write(node["text"]) out.write(" ") out.close() os.chdir("..") # go one level up in the directory tree

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!