猫史档案馆


【Python作品分享】文件管理系统【教程贴】

用户:米窝NfZ6米窝NfZ6查看:5 回复:1 评论:5 创建时间:2022-05-08T15:07:55


【作品展示】

center_image

 

【作品介绍】

将“MIWOW”改成自家电脑名,再在桌面上创建一个叫“密码登录系统”的文件夹。

 

【作品源代码】

import gzip
import sys
import time
import os
名字 = None
密码 = None


def start():
    with open("C://Users/MIWOW/Desktop/密码登录系统/time.txt", mode="r") as t:
        timea = float(t.read())
        timeb = float(time.time())
        t.close()
        # print(timea, timeb)
        # print(timeb - timea)
        # print(60 * 1440 * 10)
        if timeb - timea >= 60 * 1440 * 10:
            print("----登录过时----")
            print(" 请重新登录!!!")
            choice()
        elif timeb - timea < 60 * 1440 * 10:
            with open("C://Users/MIWOW/Desktop/密码登录系统/Last_login_account.txt", mode="r", encoding="gbk") as f:
                name = f.read()
                f.close()
                if name == "":
                    print("----登录过时----")
                    print(" 请重新登录!!!")
                    choice()
                # print(name)
                with open("C://Users/MIWOW/Desktop/密码登录系统/Name_summary.txt", mode="r", encoding="utf-8") as x:
                    名字汇总 = x.readlines()
                    # print(名字汇总)
                    x.close()
                    if 名字汇总 == []:
                        choice()
                    else:
                        Login_successful(name)


def choice():
    with open("C://Users/MIWOW/Desktop/密码登录系统/time.txt", mode="w", encoding="gbk") as f:
        f.write("1")
        print("---文件管理系统---")
        print(" 1.创建账号")
        print(" 2.登录")
        print(" 3.退出")
        print("-----------------")
        # 名字汇总 = open("C://Users/MIWOW/Desktop/密码登录系统/Name_summary.txt")
        # print(名字汇总)
        choice1 = int(input("请输入序号选择:"))
        if choice1 == 1:
            choice_one()
            # 名字 = Create_account()
            # 密码 = str(Set_password())
            # Create_user(名字, 密码)
        elif choice1 == 2:
            # print("----登录界面----")
            # Sign_in()
            choice_two()
        elif choice1 == 3:
            name = None
            Exit_the_system(name)


def choice_one():
    名字 = Create_account()
    密码 = str(Set_password())
    Create_user(名字, 密码)


def choice_two():
    print("----登录界面----")
    Sign_in()


def Create_account():
    name = input("请输入账号名:")
    with open("C://Users/MIWOW/Desktop/密码登录系统/Name_summary.txt", mode="r", encoding="utf-8") as f:
        名字汇总 = f.readlines()
        # print(名字汇总)
        f.close()
        if name + "\n" in 名字汇总:
            print("名字已被使用,请更改。")
            return Create_account()
        else:
            with open("C://Users/MIWOW/Desktop/密码登录系统/Name_summary.txt", mode="a", encoding="utf-8") as f:
                f.write(name + "\n")
                f.close()
            # Set_password()
            return name


def Set_password():
    password = input("请输入密码(密码长度4——12个数字或字母,区分大小写):")
    Password_length = len(password)
    if Password_length <= 3:
        print("密码长度过短,请重新输入。")
        return Set_password()
    elif Password_length >= 13:
        print("密码长度过长,请重新输入。")
        return Set_password()
    else:
        print("密码创建成功!")
        return password
        # Create_user(名字, 密码)


def Create_user(名字, 密码):
    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(名字), mode="w", encoding="utf-8") as f:
        f.write(密码)
        f.close()
        print("用户创建成功!")
        with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(名字 * 2), mode="a", encoding="utf-8") as n:
            n.write(名字 + "\n")
            choice()


def Sign_in():
    Number_of_attempts = 3
    # print("----登录界面----")
    with open("C://Users/MIWOW/Desktop/密码登录系统/Name_summary.txt", mode="r", encoding="utf-8") as f:
        名字汇总 = f.readlines()
        # print(名字汇总)
        f.close()
        if 名字汇总 == []:
            print("您未创建任何账号!")
            print("请至少创建一个!\n")
            choice_one()
        name = input(" 请输入用户名:")
        with open("C://Users/MIWOW/Desktop/密码登录系统/Name_summary.txt", mode="r", encoding="utf-8") as f:
            名字汇总 = f.readlines()
            # print(名字汇总)
            f.close()
            if name + "\n" in 名字汇总:
                while Number_of_attempts > 0:
                    print("您有{}次输入密码的机会。".format(Number_of_attempts))
                    password = input(" 请输入密码:")
                    Password_length = len(password)
                    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(name), mode="r", encoding="utf-8") as f:
                        密码 = str(f.read())
                        if Password_length <= 3:
                            print("密码长度过短,请重新输入。")
                            continue
                        elif Password_length >= 13:
                            print("密码长度过长,请重新输入。")
                            continue
                        elif password == 密码:
                            print("登录成功!")
                            break
                        else:
                            Number_of_attempts -= 1
                if Number_of_attempts != 0:
                    Login_successful(name)
                elif Number_of_attempts == 0:
                    print("登录失败!")
                    Sign_in()
            else:
                print(" 此用户名未登录。")
                Sign_in()


def Login_successful(name):
    os.remove("C://Users/MIWOW/Desktop/密码登录系统/Last_login_account.txt")
    with open("C://Users/MIWOW/Desktop/密码登录系统/time.txt", mode="w", encoding="gbk") as f:
        names = name * 2
        with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="a", encoding="utf-8") as a:
            a.write("")
            a.close()
            timec = str(float(time.time()))
            # print(timec)
            f.write(timec)
            f.close()
            # print(name)
            print("")
            print("----操作成功----")
            print("欢迎回来 {}".format(name))
            print(" 1.读取文件")
            print(" 2.创建文件")
            print(" 3.文件操作")
            print(" 4.退出登录")
            print(" 5.退出系统")
            print("---------------")
            # print(name)
            with open("C://Users/MIWOW/Desktop/密码登录系统/Last_login_account.txt", mode="w") as t:
                # print(name)
                t.write(name)
                t.close()
                choice1 = int(input("请输入序号选择:"))
                if choice1 > 5:
                    print("无效的操作。")
                    Login_successful(name)
                elif choice1 == 1:
                    read_file(name)
                elif choice1 == 2:
                    create_a_file(name)
                elif choice1 == 3:
                    File_operation(name)
                elif choice1 == 4:
                    print("退出成功!\n")
                    choice()
                elif choice1 == 5:
                    Exit_the_system(name)


def read_file(name):
    names = name * 2
    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="r", encoding="utf-8") as f:
        文件编号 = f.readlines()
        文件遍历 = 1
        f.close()
        # print(文件编号)
        if 文件编号 == []:
            print("您还未创建文件,快去创建吧!")
            Login_successful(name)
        print("---以下为已创建文件---")
        for x in 文件编号:
            print(" ", 文件遍历, ".{}".format(x), sep="", end="\r")
            文件遍历 += 1
        print(" ", 文件遍历, ".退出", sep="")
        文件数 = len(文件编号)
        Document_number = int(input("请输入文件编号:"))
        if Document_number == 文件遍历:
            Login_successful(name)
        elif Document_number <= 文件数:
            Document_number = str(Document_number + 1)
            file = name + Document_number
            with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(file), mode="r", encoding="utf-8") as n:
                print(n.read())
                n.close()
                read_file(name)
        else:
            print("文件不存在!")
            read_file(name)


def create_a_file(name):
    names = name * 2
    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="r", encoding="utf-8") as f:
        文件编号 = f.readlines()
        f.close()
        创建文件 = input("请输入文件名:")
        if 创建文件 + "\n" in 文件编号:
            print("此名已被使用过!")
            create_a_file(name)
        else:
            with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="a", encoding="utf-8") as s:
                s.write(创建文件 + "\n")
                s.close()
                with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="r", encoding="utf-8") as z:
                    输入 = input(r"请输入文件内容(\n代表换行,\r代表回车):")
                    编号 = str(len(z.readlines()) + 1)
                    z.close()
                    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(name + 编号), mode="w",
                              encoding="utf-8") as n:
                        n.writelines(输入)
                        n.close()
                        Login_successful(name)


def File_operation(name):
    print("----文件操作----")
    print(" 1.删除文件")
    print(" 2.文件改名")
    print(" 3.退出")
    choice1 = int(input("请输入序号选择:"))
    if choice1 > 3:
        print("无效的操作!")
        File_operation(name)
    elif choice1 == 1:
        Delete_file(name)
    elif choice1 == 3:
        Login_successful(name)
    elif choice1 == 2:
        files_renaming(name)


def Delete_file(name):
    names = name * 2
    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="r", encoding="utf-8") as f:
        文件编号 = f.readlines()
        文件遍历 = 1
        f.close()
        if 文件编号 == []:
            print("您还未创建文件,快去创建吧!")
            Login_successful(name)
        print("----文件选择----")
        for x in 文件编号:
            print(" ", 文件遍历, ".{}".format(x), sep="")
            文件遍历 += 1
        print(" ", 文件遍历, ".退出", sep="")
        文件数 = len(文件编号)
        Document_number = int(input("请输入文件编号:"))
        if Document_number == 文件遍历:
            File_operation(name)
        elif Document_number <= 文件数:
            choice1 = int(input("确定删除此文件(1 确定,0 返回):"))
            if choice1 < 0 or choice1 > 1:
                print("无效的操作!")
                File_operation(name)
            elif choice1 == 1:
                Document_number = str(Document_number + 1)
                # file = name + Document_number
                # file = name + Document_number
                # os.remove("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(file))
                with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="r", encoding="utf-8") as n:
                    文件名 = n.readlines()
                    n.close()
                    Document_number_a = int(Document_number) - 2
                    yes_or_no = False
                    # a = 0
                    for x in 文件名:
                        if int(文件名.index(x)) == Document_number_a:
                            yes_or_no = True
                            continue
                        else:
                            new_file = name + str(int(文件名.index(x)) + 2)
                            print(new_file)
                            if yes_or_no == True:
                                # print("Ture")
                                with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(new_file), mode="r",
                                          encoding="utf-8") as y:
                                    Document_content = y.read()
                                    y.close()
                                    # os.remove("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(new_file))
                                    new_file = name + \
                                        str(int(文件名.index(x)) + 1)
                                    # print(new_file)
                                    # print(Document_content)
                                    # file = name + Document_number
                                    # os.remove("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(file))
                                    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(new_file), mode="w",
                                              encoding="utf-8") as z:
                                        z.write(Document_content)
                                        z.close()
                                        # a += 1
                                        # os.remove("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(file))
                            # else:
                            #     new_file = name + str(int(文件名.index(x)) + 2)
                            #     if yes_or_no:
                            #         with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(new_file), mode="r",
                            #                   encoding="utf-8") as y:
                            #             Document_content = y.read()
                            #             y.close()
                            #             os.remove("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(new_file))
                            #             with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(new_file), mode="w",
                            #                       encoding="utf-8") as z:
                            #                 z.write(Document_content)
                            #                 z.close()
                    file = name + str(int(len(文件名)) + 1)
                    os.remove(
                        "C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(file))
                    os.remove(
                        "C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names))
                    print(文件名)
                    for x in 文件名:
                        if int(文件名.index(x)) == Document_number_a:
                            continue
                        else:
                            with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="a", encoding="utf-8") as z:
                                z.write(x)
                                z.write(""
                                        "")
                                z.close()
                    File_operation(name)
            else:
                File_operation(name)


def files_renaming(name):
    names = name * 2
    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="r", encoding="utf-8") as f:
        文件编号 = f.readlines()
        文件遍历 = 1
        f.close()
        if 文件编号 == []:
            print("您还未创建文件,快去创建吧!")
            Login_successful(name)
        print("----文件选择----")
        for x in 文件编号:
            print(" ", 文件遍历, ".{}".format(x), sep="")
            文件遍历 += 1
        print(" ", 文件遍历, ".退出", sep="")
        文件数 = len(文件编号)
        Document_number = int(input("请输入文件编号:"))
        if Document_number == 文件遍历:
            File_operation(name)
        elif Document_number > 文件数:
            print("无效的操作!")
            File_operation(name)
        else:
            Changed_file_name = input("请输入更改后的文件名:")
            # Document_number = str(Document_number + 1)
            # file = name + Document_number
            # os.rename("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(file), "C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(file))
            with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="r", encoding="utf-8") as n:
                文件名 = n.readlines()
                n.close()
                # print(Document_number)
                Document_number = int(Document_number) - 1
                # 文件名.pop(Document_number)
                文件名[Document_number] = Changed_file_name + "\n"
                os.remove("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names))
                for x in 文件名:
                    with open("C://Users/MIWOW/Desktop/密码登录系统/{}.txt".format(names), mode="a", encoding="utf-8") as z:
                        z.write(x)
                        z.close()
                File_operation(name)


def Exit_the_system(name):
    print("-欢迎再次使用文件管理系统-")
    print("  再见{}".format(name))
    print("----------------------")
    # with open("C://Users/MIWOW/Desktop/密码登录系统/Last_login_account.txt", mode="w") as t:
    #     # print(name)
    #     t.write(name)
    #     t.close()


start()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
糖醋板栗糖醋板栗

emmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm......什么意思??

点赞0


评论