python_密碼本.py

"""
    -*- conding:UTF-8 -*-

    / 我一直在找你
    / 我找到你時(shí)
    / 我就等于找到了全世界

          DOVE


"""
import os
import os.path
infors = {}  # 創(chuàng)建一個(gè)儲(chǔ)存內(nèi)容的字典


url_file = open("D:\python\password.txt", "r+")  # 打開(kāi)文件
content = url_file.readlines()  # 緩沖文件內(nèi)容到content


url_file.close()
def menu():
    """創(chuàng)建一個(gè)菜單函數(shù)"""
    print("=" * 30)
    name = "密碼本 V1.0"
    print(name.center(30))
    print("增加 【1】")
    print("刪除 【2】")
    print("修改 【3】")
    print("查找 【4】")
    print("退出 【5】")
    print("=" * 30)


def add():
    """創(chuàng)建一個(gè)添加信息函數(shù)"""

    url_file = open("D:\python\password.txt", "r+")
    input_add_name = input("請(qǐng)輸入要添加的名稱(chēng):")
    add_call_find = find(input_add_name)
    if add_call_find[0] == 0:

        input_add_url= input("請(qǐng)輸入你要添加的URL:")
        input_add_explian = input("請(qǐng)輸入U(xiǎn)RL的說(shuō)明:")
        infors["URL名稱(chēng):"] = input_add_name
        infors["URL:"] = input_add_url
        infors["URL說(shuō)明:"] = input_add_explian


        data = str(infors) + "\n"
        url_file.seek(0,2)
        url_file.write(data)
        url_file.flush()

        url_file.close()
    else:
        print("URL名稱(chēng)已經(jīng)存在!")

def pop():
    """創(chuàng)建一個(gè)刪除信息函數(shù)"""
    input_pop_name = input("請(qǐng)輸入你要?jiǎng)h除的URL名稱(chēng):")
    pop_call_find = find(input_pop_name)

    if pop_call_find[0] == 1:
        pop_file = open("D:\python\password.txt", "r")
        old_file = pop_file.readlines()

        write_file = open("D:\python\password.txt", "w")
        for temp in old_file:
            pop_content = eval(temp)
            if pop_content["URL名稱(chēng):"] == input_pop_name:
                continue
            write_file.writelines(temp)

        pop_file.flush()
        write_file.flush()

        pop_file.close()
        write_file.close()
        print("文件已刪除!")


    else:
        print("你要?jiǎng)h除的文件不存在!")


def amend():
    """創(chuàng)建一個(gè)修改函數(shù)"""

    input_amend_name = input("請(qǐng)輸入你要修改的URL名稱(chēng):")
    amend_call_find = find(input_amend_name)

    if amend_call_find[0] == 1:
        input_amend_newname = input("請(qǐng)輸入新的URL名稱(chēng):")
        input_amend_newurl = input("請(qǐng)輸入新的URL:")
        input_amend_newexplain = input("請(qǐng)輸入新的URL說(shuō)明:")
        new_amend_file = open("D:\python\[備份]password.txt", "a+")

        for old_file in content:
            old_file = eval(old_file)
            if old_file["URL名稱(chēng):"] == input_amend_name:
                infors["URL名稱(chēng):"] = input_amend_newname
                infors["URL:"] = input_amend_newurl
                infors["URL說(shuō)明:"] = input_amend_newexplain
                new_content = str(infors) + "\n"
                new_amend_file.write(new_content)

                continue
            new_content = str(old_file) + "\n"
            new_amend_file.writelines(new_content)

        new_amend_file.close()
        url_file.close()
        file_name = os.path.basename("D:\python\password.txt")
        os.chdir("D:\python")
        os.remove("D:\python\password.txt")
        os.rename("[備份]password.txt",file_name)

    else:
        print("你要修改的文件不存在!")


def find(x):
    """創(chuàng)建一個(gè)查找函數(shù)工具,找到函數(shù)名稱(chēng)返回1 沒(méi)找到返回0"""
    #input_find_name = input("請(qǐng)輸入你要查找的URL名稱(chēng):")
    input_find_name = x
    i = 0
    for eachline in content:
        str_content = eval(eachline)

        if str_content["URL名稱(chēng):"] == input_find_name:
            i = 1
            return i , str_content
            #else:
            #return i , "沒(méi)有數(shù)據(jù)"
    return i , "沒(méi)有數(shù)據(jù)"



def find_true():
    """創(chuàng)建一個(gè)查找函數(shù)"""
    input_find_true_name = input("請(qǐng)輸入你要查找的URL名稱(chēng):")

    find_true_call = find(input_find_true_name)

    if find_true_call[0] == 1:
        print("URL名稱(chēng):%s URL: %s URL說(shuō)明:%s" %(find_true_call[1]["URL名稱(chēng):"] , find_true_call[1]["URL:"] ,
                                            find_true_call[1]["URL說(shuō)明:"]))
    else:
        print("您查找的函數(shù)不存在!")

def quit():
    print("感謝你的使用! -*- ")


def main():

    while True:
        print("")
        menu()
        print("")
        input_num = int(input("請(qǐng)輸入功能號(hào):"))

        if input_num == 1:
            add()
        elif input_num == 2:
            pop()
        elif input_num == 3:
            amend()
        elif input_num == 4:
            find_true()
        elif input_num == 5:
            quit()
            break
        else:
            print("請(qǐng)輸入正確的功能號(hào):")

main()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容