-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExampleJSONforInsertDate.py
More file actions
33 lines (25 loc) · 896 Bytes
/
ExampleJSONforInsertDate.py
File metadata and controls
33 lines (25 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import json
def insert_book():
books = [] # List to store books
# Ask the user for book details
title = input("Enter the book title: ")
author = input("Enter the author's name: ")
year = input("Enter the publication year: ")
# Create a dictionary with the book's information
book = {
'Title': title,
'Author': author,
'Year': year
}
try:
with open('books.json', 'r') as file:
books = json.load(file) # Load existing data from the JSON file if any
except FileNotFoundError:
pass # If the file doesn't exist, create a new one
books.append(book) # Add the new book to the list
# Save the updated list to the JSON file
with open('books.json', 'w') as file:
json.dump(books, file, indent=4)
print("Book inserted successfully.")
if __name__ == "__main__":
insert_book()