python利用文件读写写一个博客

 代码展示

import random
import json
import time
import os


def zhuce():
    print("*********************正在注册*********************")
    try:
        users = readfile()
    except:
        fa = open(r'test.json', "w",encoding="utf-8")
        fa.write(json.dumps({"初始化": "初始化"}))
        fa.close()
        users = readfile()
        os.makedirs('用户信息')
    b = 0
    user_name = input("请输入你的用户名:")
    user_password = input("请输入你的密码:")
    for key in users.keys():
        if user_name == key:
            print("用户名已存在")
            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'test.json', "r+")
    f1 = json.load(f)
    f.close()
    return f1


def writefile(a):
    a = json.dumps(a)
    f = open(r'test.json', "w")
    f.write(a)
    f.close()


def homepage():
    print("*********************微博主页*********************")
    a = input("1注册 2登录 3退出\n请输入你的选择:")
    return a


def zhucetexiao():
    print("注册中,请稍后!")
    print("更新成功!")
    print("注册成功!")
    print("默认文章表创建完成!")


def denglu():
    b = 0
    print("*********************正在登录*********************")
    users = readfile()
    user_name = input("登录用户名:")
    user_password = input("登录密码:")
    for key in users.keys():
        if user_name == key:
            print("用户存在,判断密码")
            b = 2
            if user_password == users[key]:
                print("登陆成功")
                b = 1
                dengluxiaoguo(user_name)
                break
    if b == 0:
        print("登录失败")
    return b


def dengluxiaoguo(user_name):
    while 0 == 0:
        a = input("1写文章 2读取所有 3读取一篇 4编辑一篇 5删除一篇 6登出\n请输入你的选择:")
        users = article_read(user_name)
        if a == "1":
            print("*********************写一篇文章*********************")
            article_write(user_name)
        elif a == "2":
            duqusuoyou(user_name)
        elif a == "3":
            print("*********************读一篇文章*********************")
            duquyipian(user_name)
        elif a == "4":
            print("*********************改一篇文章*********************")
            bianjiyipian(user_name)
        elif a == "5":
            print("*********************删一篇文章*********************")
            shanchuyipian(user_name)
        elif a == "6":
            break


def shanchuyipian(user_name):
    duqusuoyou(user_name)
    a = input("请输入您要删除的序号?")
    f = open(f'用户信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    if len(f1) > 1:
        if int(a) > 0 and int(a) <= len(f1):
            f1.pop(a)
            for i in range(1, len(f1) + 2):
                if i > int(a):
                    f1[str(i - 1)] = f1.pop(str(i))
            f = open(f'用户信息\{user_name}.json', "w")
            b = json.dumps(f1)
            f.write(b)
            f.close()
        else:
            print("未找到该序列")
    else:
        print("最后一篇文章拒绝删除")
    duqusuoyou(user_name)


def bianjiyipian(user_name):
    duqusuoyou(user_name)
    a = input("请输入您要编辑的序号?")
    f = open(f'用户信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    if int(a) > 0 and int(a) <= len(f1):
        article_name = input("文章名称")
        article_content = input("文章内容")
        f = open(f'用户信息\{user_name}.json', "r+")
        f1 = json.load(f)
        f.close()
        f = open(f'用户信息\{user_name}.json', "w")
        time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        f1[a] = {"title": article_name, "time": time1, "author": user_name, "content": article_content}
        b = json.dumps(f1)
        f.write(b)
        f.close()
    else:
        print("未找到该序列")


def duquyipian(user_name):
    duqusuoyou(user_name)
    a = input("请输入您要查看的序号?")
    f = open(f'用户信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    if int(a) > 0 and int(a) <= len(f1):
        print(f1[a])
    else:
        print("未找到该序列")


def duqusuoyou(user_name):
    print("*********************文章目录*********************")
    f = open(f'用户信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    for key in f1.keys():
        print(f"{key} {f1[key]['title']}        {f1[key]['time']}")


def article_read(user_name):
    f = open(f'用户信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    return f1


def article_write(user_name):
    f = open(f'用户信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    key_count = len(f1) + 1
    article_name = input("文章名称:")
    article_content = input("文章内容:")
    time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f = open(f'用户信息\{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 = json.dumps(f1)
    f.write(b)
    f.close()


def readfile1():
    f = open(r'test1.json', "r+")
    f1 = json.load(f)
    f.close()
    return f1


def writefile1(a):
    time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f = open(f'用户信息\{a}.json', "w")
    b = {1: {"title": "初始化文章", "time": time1, "author": "0", "content": "0"}}
    print(b)
    b = json.dumps(b)
    f.write(b)
    f.close()


while 0 == 0:
    h = homepage()
    if h == "1":
        if zhuce() == 0:
            zhucetexiao()
    elif h == "2":
        if denglu() == 1:
            pass
    else:
        exit()






文件保存样式:

 运行结果:

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值