python 控制台版文章管理系统作业

控制台版文章管理系统:
1、用户管理系统
2、文章的管理和控制
3、宠物管理【扩展功能】

|-- 密码加密
|-- 时间API
|-- 实现数据的持久化
import json
import sys
import os
import time
import hashlib


def login (login_ls):
    """登录过程"""
    name = input('请输入用户名:')
    if name in login_ls:
        password = int(input('请输入密码:'))
        if password == int(login_ls[name]):
            print('登录成功!')
            return True
        else:
            print('密码错误!')
            return False
    else:
        print('该用户不存在!')
        return False


def get_md5 (password):
    """密码加密"""
    password = str(password)
    password1 = hashlib.md5()
    password1.update(password.encode("utf8"))
    password = password1.hexdigest()
    return password


def enroll ():
    """注册过程"""
    enroll_ls = dict()
    name = input('请输入注册的用户名:')
    password = int(input('请输入密码:'))
    if input('是否注册') == 'yes' or input('是否注册') == 'Yes':
        # password = hashlib.md5().update(password)
        change = input('是否加密')
        if change == 'yes' or change == 'Yes':
            password = get_md5(password)
        enroll_ls[name] = password
        print('注册成功!')
        return enroll_ls
    else:
        print('注册失败!')
        return enroll_ls


def write_article (article_new):
    """继续写文章"""
    while True:
        with open(article_new, "a") as fle:
            w = fle.write(input('请输入:')+'\n')
            fle.flush()
        if w == 0:
            break


def read_article (article_new):
    """读取文章"""
    with open(article_new, "r") as fle:
        w = fle.readlines()
        for word in w:
            print(word)



def get_ls ():
    article = os.listdir("D:\\python\\IO\\ls")  # 获取文章目录
    print('文章目录')

    for i in range(0,len(article)):
        print(f'{i+1}.{article[i]}')

    option = int(input('输入要查找的文件的数字:'))
    article_new = os.path.join("D:\\python\\IO\\ls", article[option-1])
    now_time = time.strftime("%Y-%m-%d %H:%M:%S")  # 获取当前时间
    old_time = time.ctime(os.path.getctime(article_new))  # 获取文件的创建时间
    change_time = time.ctime(os.path.getmtime(article_new))  # 获取上一次修改时间
    article_dig = os.path.getsize(article_new)  # 获取文件大小
    print(f'文件:{article[option]}\n文件大小{article_dig}\n当前时间{now_time}')
    print(f'文件创建时间{old_time}\n文件修改时间{change_time}')
    read_article(article_new)
    option1 = input('是否继续')

    if option1 == 'yes' or 'Yes':
        write_article(article_new)




if __name__ == "__main__":
    print(' ' * 10 + '登录界面')
    print('~ * ' * 10)
    print(' ' * 12 + '1. 用户登录\n' + ' ' * 12 + '2. 新用户注册\n' + ' ' * 12 + '3. 退出系统')
    print('~ * ' * 10)
    option1 = int(input('(温馨提示)请输入您的选项:'))
    if option1 == 1:
        ls = open("D:\\python\\IO\\ls.txt", 'r')
        login_ls = json.load(ls)
        print(login_ls)
        ls.close()

        if login(login_ls):
            get_ls()


    elif option1 == 2:
        '''
        ls = open("D:\\python\\IO\\ls.txt", 'a')
        enroll_ls = json.load(ls)
        print(enroll_ls)
        '''
        enroll_dict = enroll()
        # ls = open("D:\\python\\IO\\ls.txt", 'w')
        # enroll_dict = enroll(enroll_ls)
        # ls.write(enroll_dict)
        json.dump(enroll_dict, open("D:\\python\\IO\\ls.txt", 'w'))
        # ls.close()


    elif option1 == 3:

        sys.exit()

    '''
    elif option1 == 4:
        """测试命令"""
        
    '''

你可能感兴趣的:(python学习,python)