Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions