Management System in Python - #4 - Finding Records & Resetting Files

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...
  1. elif (user is 4):
  2. findUser();
Update the instructions...
  1. 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...
  1. def findUser():
  2. user = input("Enter the information to search for... (exit when done. ")
  3. information = ""
  4. while (user.lower() != 'exit'):
  5. if (information > ""):
  6. information += "\t" + user.lower()
  7. else:
  8. information += user.lower() #First information, no tab needed
  9. user = input("Enter more information or 'exit'... ")
  10. for record in getMatchedRecords(information):
  11. print(record)
Clearing Files: Next we have the clearing/resetting the file option. First add the option to the main menu system...
  1. elif (user is 11):
  2. resetFile()
Update the instructions...
  1. 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.
  1. def resetFile():
  2. file = open(fileName, 'w')
  3. file.write('')
  4. file.close()

Add new comment