This article example for you to share the python use file read and write a blog specific code for your reference, the details are as follows
Code Showcase
import random import json import time import os def zhuce(): print("********************* is registering *********************") try: users = readfile() except: fa = open(r'', "w",encoding="utf-8") (({"Initialization": "Initialization"})) () users = readfile() ('User information') b = 0 user_name = input("Please enter your user name:") user_password = input("Please enter your password:") for key in (): if user_name == key: print("User name already exists") b = 1 break if b == 0: writefile1(user_name) users[user_name] = user_password writefile1(user_name) writefile(users) return b def readfile(): f = open(r'', "r+") f1 = (f) () return f1 def writefile(a): a = (a) f = open(r'', "w") (a) () def homepage(): print("********************* twitter homepage *********************") a = input("1Register 2Login 3Logout\nPlease enter your choice:") return a def zhucetexiao(): print("Registration in progress, please wait!") print("Update successful!") print("Registration successful!") print("Default article table creation complete!") def denglu(): b = 0 print("********************* is logging on to *********************") users = readfile() user_name = input("Login User Name:") user_password = input("Login Password:") for key in (): if user_name == key: print("User exists. Determining password.") b = 2 if user_password == users[key]: print("Landing successful.") b = 1 dengluxiaoguo(user_name) break if b == 0: print("Login failed.") return b def dengluxiaoguo(user_name): while 0 == 0: a = input("1 Write article 2 Read all 3 Read one 4 Edit one 5 Delete one 6 Log out \n Please enter your choice:") users = article_read(user_name) if a == "1": print("********************* write an article *********************") article_write(user_name) elif a == "2": duqusuoyou(user_name) elif a == "3": print("********************* Read an article at *********************") duquyipian(user_name) elif a == "4": print("********************* change an article *********************") bianjiyipian(user_name) elif a == "5": print("********************* delete an article *********************") shanchuyipian(user_name) elif a == "6": break def shanchuyipian(user_name): duqusuoyou(user_name) a = input("Please enter the serial number you want to delete?") f = open(f'user information\{user_name}.json', "r+") f1 = (f) () if len(f1) > 1: if int(a) > 0 and int(a) <= len(f1): (a) for i in range(1, len(f1) + 2): if i > int(a): f1[str(i - 1)] = (str(i)) f = open(f'user information\{user_name}.json', "w") b = (f1) (b) () else: print("Sequence not found.") else: print("Last post refused to be deleted.") duqusuoyou(user_name) def bianjiyipian(user_name): duqusuoyou(user_name) a = input("Please enter the serial number you want to edit?") f = open(f'user information\{user_name}.json', "r+") f1 = (f) () if int(a) > 0 and int(a) <= len(f1): article_name = input("Name of article") article_content = input("Article Content") f = open(f'user information\{user_name}.json', "r+") f1 = (f) () f = open(f'user information\{user_name}.json', "w") time1 = ("%Y-%m-%d %H:%M:%S", ()) f1[a] = {"title": article_name, "time": time1, "author": user_name, "content": article_content} b = (f1) (b) () else: print("Sequence not found.") def duquyipian(user_name): duqusuoyou(user_name) a = input("Please enter the serial number you wish to view?") f = open(f'user information\{user_name}.json', "r+") f1 = (f) () if int(a) > 0 and int(a) <= len(f1): print(f1[a]) else: print("Sequence not found.") def duqusuoyou(user_name): print("********************* Article Directory *********************") f = open(f'user information\{user_name}.json', "r+") f1 = (f) () for key in (): print(f"{key} {f1[key]['title']} {f1[key]['time']}") def article_read(user_name): f = open(f'user information\{user_name}.json', "r+") f1 = (f) () return f1 def article_write(user_name): f = open(f'user information\{user_name}.json', "r+") f1 = (f) () key_count = len(f1) + 1 article_name = input("Article Title:") article_content = input("Article Content:") time1 = ("%Y-%m-%d %H:%M:%S", ()) f = open(f'user information\{user_name}.json', "w") # print(type(key_count)) f1[str(key_count)] = {"title": article_name, "time": time1, "author": user_name, "content": article_content} print(f1) b = (f1) (b) () def readfile1(): f = open(r'', "r+") f1 = (f) () return f1 def writefile1(a): time1 = ("%Y-%m-%d %H:%M:%S", ()) f = open(f'user information\{a}.json', "w") b = {1: {"title": "Initializing Articles", "time": time1, "author": "0", "content": "0"}} print(b) b = (b) (b) () while 0 == 0: h = homepage() if h == "1": if zhuce() == 0: zhucetexiao() elif h == "2": if denglu() == 1: pass else: exit()
File saving style:
Run results:
This is the whole content of this article.