Web site account password management program implemented in python

I recently started learning python, and after learning the basics, I wrote some small programs to consolidate knowledge points. What is shared here is a URL account password management program implemented in python, which realizes the functions of receiving user input, writing information to files, and querying the entered information. It uses the basics of strings, lists, dictionaries, and functions in python. Knowledge.

Welcome all the great gods to complain~~~

# import package
import them
#import sys

#Get the path where the program is located
path = os.getcwd()

if not os.path.exists(path + '\\siteinfos.txt'):
    txt = open(path + '\\siteinfos.txt','w')

#Open the file under path, read and write mode: w+, if the file does not exist, it will be created automatically
txt = open(path + '\\siteinfos.txt')

temp = txt.read()
txt.close()

if temp != '':
    temp = eval(temp)
    # output the contents of the current file
    print('The contents of the current file are as follows:')
    print(temp)
else:
    temp = []
    print('There is no content in the current file!')

#sys.exit()

#Define a list, each item in the list stores a dictionary, and the dictionary stores website information according to the defined items (URL, username and password, etc.)

#Get a list (new or read)
#temp = ["{'web':'www.jd.com','name':'zsc','passwd':'qaz'}","{'web':'www.baidu.com','name':'zsc','passwd':'qaz'}", "{'web':'www.taobao.com','name':'zsc','passwd':'qaz'}" ]

#Define a check function to check whether the URL web already has a record?
def checkrecord(web):
    #Define a boolean variable to store the check result
    isIn = False
    # Traverse the dictionary in the list to see if the URL web exists
    for i in range(len(temp)):
        #Format each item of the list into a dictionary to determine whether the user exists
        if eval(temp[i - 1]).get('web', 0) == web:
            #If it exists, isIn is True and exits the check
            isIn = True
            break
        else:
            #If it is false, isIn is False, continue to check
            isIn = False
    if isIn:
        #URL already exists, return True
        return True
    else:
        #User does not exist, return False
        return False
    
#Define a display function to display all records under the URL web
def showrecord(web):
    # Traverse the dictionary in the list to see if the URL web exists
    for i in range(len(temp)):
        #Format each item of the list into a dictionary to determine whether the user exists
        if web in eval(temp[i - 1]).values():
            #Exist, output the record
            print(temp[i - 1])

#define a create function to add new records to the list
def newtable(web,name,passwd):
    #Use the try...except function to catch exceptions to improve user experience
    try:
        #Get the length of the list, used to locate the subscript when inserting
        changdu = len(temp)
        #spelling into a dictionary
        temp_dic = "{'web':'" + web + "','name':'" + name + "','passwd':'" + passwd + "'}"
        #Add the value with the insert() method of the list
        temp.insert(changdu + 1,  temp_dic)
    except:
        #Output exception when an exception is encountered during the addition process
        #print('Add encountered an unknown error!')
        #return False
        return False
    else:
        #Added successfully
        #print('Added successfully!')
        #return True
        txt = open(path + '\\siteinfos.txt','w+')
        txt.write(str(temp))
        txt.close()
        #print('Bir database updated.')
        return True

while True:
    try:
        web = input('Please enter the URL:')
        if web != '':
            if checkrecord(web):
                print('URL already exists!')
                #Display the account of the website that already exists in the current list
                showrecord (web)
                goon = input('Do you want to continue adding users to this website? [y/n](default:y)')
                if goon == '' or goon == 'y':
                    name = input('Please enter the account number:')
                    passwd = input('Please enter the password:')
                    if name != '' and passwd != '':
                        if newtable(web,name,passwd):
                            #Prompt added successfully
                            print('Added successfully!')
                            #Display the account of the website that already exists in the current list
                            showrecord (web)
                            #Prompt whether to continue adding
                            goon = input('Do you want to continue adding? [y/n](default:y)')
                            if goon != '' and goon != 'y':
                                print('Goodbye!')
                                break
                        else:
                            print('Add failed!')
                    elif name != '' or passwd != '':
                        print('URL, account or password cannot be empty!')
                    else:
                        print('Goodbye!')
                        break
                else:
                    print('Goodbye!')
                    break
            else:
                    name = input('Please enter the account number:')
                    passwd = input('Please enter the password:')
                    if name != '' and passwd != '':
                        if newtable(web,name,passwd):
                            #Prompt added successfully
                            print('Added successfully!')
                            #Display the account of the website that already exists in the current list
                            showrecord (web)
                            #Prompt whether to continue adding
                            goon = input('Do you want to continue adding? [y/n](default:y)')
                            if goon != '' and goon != 'y':
                                print('Goodbye!')
                                break
                        else:
                            print('Add failed!')
                    elif name != '' or passwd != '':
                        print('URL, account or password cannot be empty!')
                        
    except KeyboardInterrupt:
        print('Byte!')
        break

Test screenshot:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324628388&siteId=291194637