入门博客程序 (实现登录,注册,查看,更新,删除)

本文介绍了一个简易的博客管理系统,包括用户注册、登录、文章撰写、查看、修改等功能,并使用Python实现这些功能。系统还具备基本的数据持久化能力,利用JSON文件保存用户信息和文章内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import os,json,time,random,sys
from datetime import datetime

第一次 创建 配置文件

def createuser(path=“博客.json”):

if not os.path.exists(path):
    temp={"博客":[{"acc":"546280927","pas":"123612"}]}   
    with open(path,"w") as f_w:
        json.dump(temp,f_w)
        c_article("546280927")
        print("创建成功")
else:
    print("文件已存在,创建失败")

读取用户数据

def readuser(path=“博客.json”):
if os.path.exists(path):
with open(path,“r”) as f_r:
return json.load(f_r)

用于更新数据

def updateuser(unpdate,path=“博客.json”):
with open(path,“r”) as x:
temp=json.load(x)
temp[“博客”].append(unpdate)
with open(path,“w”) as y:
json.dump(temp,y)
print(“更新成功”)

登录

def login(path=“博客.json”):
global globalName
print(“登录”.center(50,"*"))
name=input(“请输入用户名:”)
mima=input(“请输入密码:”)

temp=readuser(path)             # 运用读取方法(上面写过)
for i in temp["博客"]:
    if i["acc"]==name and i["pas"]==mima:
        print("登录成功")
        globalName=name
        return True
else:
    print("账号或密码错误")
    return False

注册

def rigister(path=“博客.json”):
print(“注册”.center(50,"*"))
name=input(“请输入账号:”)
mima=input(“请输入密码:”)
temp=readuser(path) #用方法读取数据
for i in temp[“博客”]:
if i[“acc”]==name: #比较是否有这个账号
print(“此账号已被注册”)
return False
else:
updateuser({“acc”:name,“pas”:mima},path) #用更新的方法添加新的数据
print(“注册成功”)
c_article(name)
return True

创建默认文章表!

def c_article(name,path=“article”):
if not os.path.exists(path):
os.mkdir(path)

with open(os.path.join(path,name+".json"),"w") as f_w:
    temp={"article":[{"题目":"默认",
                      "作者":"默认",
                      "时间":datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                     "内容":"默认内容" }]}   

    json.dump(temp,f_w)
    print("创建默认文章成功")

读取文章表

def read_article(name,path=“article”):
newarticle=os.path.join(path,name+".json")#把文件夹的路径和文件夹下文件的路径合并

with open(newarticle,"r") as f_r:
    return json.load(f_r)

写文章

def write_article(name,path=“article”):
newarticle = os.path.join(path, name + “.json”)#把文件夹的路径和文件夹下文件的路径合并
print(“写文章”.center(50,"*"))
biao=input(“标题:”)
nei=input(“内容: “)
temp={“article”:[{“题目”:biao,
“作者”:name,
“时间”:datetime.now().strftime(”%Y-%m-%d %H:%M:%S”),
“内容”:nei }]} #写出框架模式,把用户填写的内容给填充进去
temp2=readuser(newarticle) #用方法读取文件
temp2[“article”].append(temp) #把新的内容加入旧的内容
with open(newarticle,“w”) as f_w:
json.dump(temp2,f_w) #把全新的内容写入文件内
print(“写入成功”)

查询文章,列出所有文章及时间

def inquire(name,path=“article”):
all=read_article(name,path=“article”)
print(f"{name}的所有文章!".center(50, “*”))
for i in range(len(all[“article”])):
print(i+1,all[“article”][i][“题目”]+"–"+all[“article”][i][“时间”])

print("end".center(50,"*"))

查看其中一篇文章

def examine(name,path=“article”):
inquire(name)#列出name的所有文章让客户选择
num=int(input(“请输入你要查看的文章的序列号:”))
table=read_article(name)#读取name的所有文章

temp=table["article"][num-1]
print("题目",temp["题目"])
print("作者", temp["作者"])
print("时间", temp["时间"])
print("内容", temp["内容"])

print("*"*50)

删除文章

def del_article(name,path=“article”):
inquire(name)#列出name的所有文章让客户选择
num=int(input(“请输入你要删除的文章的序列号:”))
table=read_article(name)#读取name的所有文章
table[“article”].pop(num-1)#删除掉不要的
newpath=os.path.join(path,name+".json")
with open(newpath,“w”) as f_w:
json.dump(table,f_w)
print(“删除成功”)

修改文章

def alter(name,path=“article”):
inquire(name)
num=int(input(“请输入你要修改的文章的序列号:”))
title=input(“请输入你要修改的题目——>”)
content=input(“请输入你要修改的内容——>”)
table=read_article(name) #读取要修改的文章
temp=table[“article”][num-1]
temp[“题目”]=title
temp[“时间”]=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
temp[“内容”]=content
newpath = os.path.join(path, name + “.json”)#路径的合并
with open(newpath,“w”) as f_w:
json.dump(temp,f_w)
print(“修改成功”)

美丽的分割线

#******************************************************************

createuser()
print(“欢迎来到豪华VIPblog”.center(50,""))
globalName=""
while True:
print("********************")
print("
1:登录 “)
print(”
2:注册 “)
print(”
3:退出 ")
print(“")
x=int(input(“请选择——>”))
if x1:
if login():
while True:
print("
***********")
print("
1:写文章 “)
print(”
2:查看所有 “)
print(”
3:查看一篇 “)
print(”
4:删除 “)
print(”
5:修改 “)
print(”
6:退出 “)
print(”
********************************”)
y=int(input(“请选择——>”))
if y1:
write_article(globalName)
elif y2:
inquire(globalName)
elif y
3:
examine(globalName)
elif y4:
del_article(globalName)
elif y
5:
alter(globalName)
elif y==6:
break
else:
print(“输入有误!”)

elif x==2:
    rigister()
else:
    sys.exit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值