import datetime FIELDS = ['title', 'subtitle', 'content', 'displayall'] def add_entry(file_path, title, subtitle, content, display_all=False): # 1. Import the current list to get the latest ID # We use a namespace to avoid path issues namespace = {} with open(file_path, 'r') as f: exec(f.read(), namespace) posts = namespace.get('BLOG_POSTS', []) new_id = max([p['id'] for p in posts], default=0) + 1 print("new id", new_id) # 2. Format the new entry string date_str = datetime.datetime.now().strftime("%B %d, %Y") new_post_str = f""" {{ 'id': {new_id}, 'title': '{title}', 'content': \"\"\" {content} \"\"\", 'subtitle': '{subtitle}', 'date': '{date_str}', "displayall": {display_all} }}, """ # 3. Append to the file # Note: This appends to the END of the file. # If your file ends with ']', we need to strip that first. with open(file_path, 'r+') as f: content_full = f.read().strip() # Find the last closing bracket of the list if content_full.endswith(']'): content_full = content_full.rsplit(']', 1)[0] f.seek(0) f.write(content_full + new_post_str + "\n]") f.truncate() print(f"Successfully added Post #{new_id}: {title}") # Usage if __name__ == "__main__": #t = input("Title: ") #s = input("Subtitle: ") #c = input("Content (HTML): ") # title t = "I have a solution but it only works for spherical chickens in a vacuum!" # subtitle s = "" # content c = "no content" add_entry('content/posts.py', t, s, c)