Management System in Python - #2 - Settings Menu, File Path Setting & Listing Users

Submitted by: 
Visitors have accessed this post 227 times.

In this tutorial we will continue making our local user management system in Python. In part one we made the main menu system, and the first option to add a user. This tutorial will be on a second option, listing users, and a settings option to change the file name.

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.

Listing Users:
So we want to add a new option (2) to our main menu system loop to allow the user to select to list the current records within the users...

  1. elif (user is 2):
  2. listUsers();

So that gets added on to the other options...

  1. def main():
  2. print("Welcome to my local file user management system [Created by Yorkiebar]. There are two options; 0=exit, 1=add user, 2=list users...\n");
  3. going = 'y';
  4. while (going is 'y'):
  5. try:
  6. user = int(input('Enter your option: '));
  7. except ValueError:
  8. print('That value is not legal. Please try again. 0=exit.');
  9. if (user is 0):
  10. going = '0';
  11. elif (user is 1):
  12. addUser();
  13. elif (user is 2):
  14. listUsers();

listUsers Function:
As you can see from the main menu system loop, the second option - listing users - runs a new function we are yet to create named listUsers. Lets create this now...

  1. def listUsers():
  2. file = open('management.txt', 'r');
  3. lines = file.readlines();
  4. for line in lines:
  5. print(line);
  6. file.close();

So first we open another new file stream to the records file, with the access mode as 'r' standing for read since we do not need to edit the information, only read/list it.

Then we create a new tuple named lines and set its value to the tuple returned by the readlines function on the file - returns each line one by one from the file.

Finally we loop through each entry in to our lines tuple (each line in the file), print it out to the program console, and close the file stream - again, we close the file stream to avoid any reading/writing errors to/from the file later on, even in other applications.

Settings - File Name:
Next we are creating a settings option (10) for the user to change the file name...

  1. elif (user is 10):
  2. setFileName()

First we add another new option to the main menu system loop (10) which calls the function we are yet to create named setFileName. Now lets create this function...

  1. def setFileName():
  2. global fileName
  3. user = input("Enter a new file name... ");
  4. while (user == ''):
  5. user = input('That file name was not valid, please enter another one, include the extension (.txt for example).');
  6. if (user.lower() != 'exit'):
  7. fileName = user
  8. print('New file name set to: ' + fileName)
  9. else:
  10. print('You have chosen to not set a new file name. Still using: ' + fileName)

So the above function gets a variable from the global scope - fileName, we create this in a second - then takes a user input. While the user input is nothing, we ask them for a valid filename. Then we check if they have entered 'exit', if not we set the fileName variable to the entered file name. We output a result.

So now we need to create the global variable fileName and replace any paths with it. So we create it at the top of the document, under any imports - we have none at the moment...

  1. fileName = 'management.txt'

-We give it a default value of the management.txt filename path.-

Next we replace any paths we use hardcoded in to the open functions used to create file streams with the fileName global variable...

  1. def addUser():
  2. global fileName
  3. information = []
  4. user = input('Enter information or "exit"... ')
  5. while (user.lower() != 'exit'):
  6. information.append(user)
  7. user = input('Enter information or "exit"...')
  8. print('Adding information to file...')
  9. file = open(fileName, 'a')
  10. lineWrite = ""
  11. for line in information:
  12. lineWrite += line + "\t"
  13. lineWrite += "\n"
  14. file.write(lineWrite)
  15. print('Wrote information successfully.')
  16. file.close()
  18. def listUsers():
  19. global fileName
  20. file = open(fileName, 'r')
  21. lines = file.readlines()
  22. for line in lines:
  23. print(line)
  24. file.close()

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.