猫史档案馆


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

用户:米窝NfZ6米窝NfZ6查看:0 回复:1 评论:0 创建时间:2022-06-04T17:20:31


【作品展示】

center_image

 

【作品介绍】

一个可以进行创建账号、登录、创建文件、读取文件等功能的小程序!!!

这是我小学阶段最成功的自己写的程序!

 

【作品源代码】

import gzip
import sys
import time
import os
import getpass
名字 = None
密码 = None
name_1 = getpass.getuser()


def start():
    try:
        os.listdir("C://Users/{}/Desktop/密码登录系统".format(name_1))
    except Exception as error:
        print(error)
        os.mkdir("C://Users/{}/Desktop/密码登录系统".format(name_1))
        with open("C://Users/{}/Desktop/密码登录系统/Last_login_account.txt".format(name_1),
                  mode="w", encoding="gbk") as f:
            f.write("")
            f.close()
            with open("C://Users/{}/Desktop/密码登录系统/Name_summary.txt".format(name_1),
                      mode="w", encoding="utf-8") as x:
                x.write("")
                x.close()
                with open("C://Users/{}/Desktop/密码登录系统/time.txt".format(name_1),
                          mode="w", encoding="utf-8") as y:
                    y.write("0")
                    y.close()
                    start()
    else:
        with open("C://Users/{}/Desktop/密码登录系统/time.txt".format(name_1), mode="r") as t:
            timea = float(t.read())
            timeb = float(time.time())
            t.close()
            if timeb - timea >= 60 * 1440 * 10:
                print("----登录过时----")
                print(" 请重新登录!!!")
                choice()
            elif timeb - timea < 60 * 1440 * 10:
                with open("C://Users/{}/Desktop/密码登录系统/Last_login_account.txt".format(name_1),
                          mode="r", encoding="gbk") as f:
                    name = f.read()
                    f.close()
                    if name == "":
                        print("----登录过时----")
                        print(" 请重新登录!!!")
                        choice()
                    with open("C://Users/{}/Desktop/密码登录系统/Name_summary.txt".format(name_1),
                              mode="r", encoding="utf-8") as x:
                        名字汇总 = x.readlines()
                        x.close()
                        if 名字汇总 == []:
                            choice()
                        else:
                            Login_successful(name)


def choice():
    with open("C://Users/{}/Desktop/密码登录系统/time.txt".format(name_1), mode="w", encoding="gbk") as f:
        f.write("1")
        print("---文件管理系统---")
        print(" 1.创建账号")
        print(" 2.登录")
        print(" 3.退出")
        print("-----------------")
        choice1 = int(input("请输入序号选择:"))
        if choice1 == 1:
            choice_one()
        elif choice1 == 2:
            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/{}/Desktop/密码登录系统/Name_summary.txt".format(name_1), mode="r", encoding="utf-8") as f:
        名字汇总 = f.readlines()
        f.close()
        if name + "\n" in 名字汇总:
            print("名字已被使用,请更改。")
            return Create_account()
        else:
            if len(name) < 1:
                print("名字不符合规范!!!")
                Create_account()
            with open("C://Users/{}/Desktop/密码登录系统/Name_summary.txt".format(name_1), mode="a", encoding="utf-8") as f:
                f.write(name + "\n")
                f.close()
            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


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


def Sign_in():
    Number_of_attempts = 3
    with open("C://Users/{}/Desktop/密码登录系统/Name_summary.txt".format(name_1), mode="r", encoding="utf-8") as f:
        名字汇总 = f.readlines()
        f.close()
        if 名字汇总 == []:
            print("您未创建任何账号!")
            print("请至少创建一个!\n")
            choice_one()
        name = input(" 请输入用户名:")
        with open("C://Users/{}/Desktop/密码登录系统/Name_summary.txt".format(name_1), mode="r", encoding="utf-8") as f:
            名字汇总 = f.readlines()
            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/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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/{}/Desktop/密码登录系统/Last_login_account.txt".format(name_1))
    with open("C://Users/{}/Desktop/密码登录系统/time.txt".format(name_1), mode="w", encoding="gbk") as f:
        names = name * 2
        with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, names), mode="a", encoding="utf-8") as a:
            a.write("")
            a.close()
            timec = str(float(time.time()))
            f.write(timec)
            f.close()
            print("")
            print("----操作成功----")
            print("欢迎回来 {}".format(name))
            print(" 1.读取文件")
            print(" 2.创建文件")
            print(" 3.文件操作")
            print(" 4.退出登录")
            print(" 5.退出系统")
            print("---------------")
            with open("C://Users/{}/Desktop/密码登录系统/Last_login_account.txt".format(name_1), mode="w") as t:
                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/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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="", 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/{}/Desktop/密码登录系统/{}.txt".format(name_1, file), mode="r", encoding="utf-8") as n:
                print("\n", n.read())
                n.close()
                file_a = input("是否结束:")
                print(file_a)
                read_file(name)
        else:
            print("文件不存在!")
            read_file(name)


def create_a_file(name):
    names = name * 2
    with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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/{}/Desktop/密码登录系统/{}.txt".format(name_1, names), mode="a", encoding="utf-8") as s:
                s.write(创建文件 + "\n")
                s.close()
                with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, names), mode="r", encoding="utf-8") as z:
                    输入 = input(r"请输入文件内容(\n代表换行,\r代表回车):")
                    编号 = str(len(z.readlines()) + 1)
                    z.close()
                    with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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)
                with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, names),
                          mode="r", encoding="utf-8") as n:
                    文件名 = n.readlines()
                    n.close()
                    Document_number_a = int(Document_number) - 2
                    yes_or_no = False
                    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:
                                with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, new_file), mode="r",
                                          encoding="utf-8") as y:
                                    Document_content = y.read()
                                    y.close()
                                    new_file = name + \
                                        str(int(文件名.index(x)) + 1)
                                    with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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/{}/Desktop/密码登录系统/{}.txt".format(name_1, file))
                    os.remove(
                        "C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, names))
                    print(文件名)
                    for x in 文件名:
                        if int(文件名.index(x)) == Document_number_a:
                            continue
                        else:
                            with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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("请输入更改后的文件名:")
            with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, names), mode="r", encoding="utf-8") as n:
                文件名 = n.readlines()
                n.close()
                Document_number = int(Document_number) - 1
                文件名[Document_number] = Changed_file_name + "\n"
                os.remove(
                    "C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, names))
                for x in 文件名:
                    with open("C://Users/{}/Desktop/密码登录系统/{}.txt".format(name_1, 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("----------------------")


start()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
米窝NfZ6米窝NfZ6

修改了之前无法在其他电脑上使用的问题,基本上没有发现问题了!!

点赞3


评论