Introduction:
In this tutorial we will continue making our local user management system in Python. In part three we made the remove record function which paired with a lot of other new functions.
Important:
Although I keep interchanging the words records and users, this system can be used to keep any records of information, but I am using this example as a user management system.
Now:
This tutorial we are going to make a search/find option, and a clean file option.
Search/Find Options:
Since in the previous we made a function named getMatchedRecords to return the records matching a certain string or piece of information entered by the user, we can use this to efficiently work as a search/find function as well. First add the choice...
elif (user is 4):
findUser();
Update the instructions...
print("Welcome to my local file user management system [Created by Yorkiebar]. There are the following options; 0=exit, 1=add user, 2=list users, 3=remove user, 4=find user, 10=change file path...\n")
Now for the findUser function. We take information from the user through a while loop (while they haven't entered the word 'exit' (without single quotes)) then we print each record returns from the getMatchedRecords function while parsing the information line...
def findUser():
user = input("Enter the information to search for... (exit when done. ")
information = ""
while (user.lower() != 'exit'):
if (information > ""):
information += "\t" + user.lower()
else:
information += user.lower() #First information, no tab needed
user = input("Enter more information or 'exit'... ")
for record in getMatchedRecords(information):
print(record)
Clearing Files:
Next we have the clearing/resetting the file option. First add the option to the main menu system...
elif (user is 11):
resetFile()
Update the instructions...
print("Welcome to my local file user management system [Created by Yorkiebar]. There are the following options; 0=exit, 1=add user, 2=list users, 3=remove user, 4=find user, 10=change file path, 11=reset file...\n")
As you can see the new option/choice runs a function named resetFile. This function simply creates a new writing file stream to open the file path, writes a blank string, and closes/saves the stream. Simple.
def resetFile():
file = open(fileName, 'w')
file.write('')
file.close()