2

I am new to python (and programming in general) and am making a database/register for a typical class. I wanted the user to be able to add and remove pupils from the database, I used lists primarily for this but have hit a stump.

Whenever I restart the program the list the user has modified returns back to the defualt list I specified in the code. I looked around the internet and tried to save the list onto a seperate txt file. However the txt file also goes back to the defualt every time I restart the program. I would like you to please give me a way to save the changes made to the list and keep them that way. Here is the code (it's not very good):

def menu():
    print "*****************CLASS REGISTER*****************"
    print "Press 1 See The List Of Pupils"
    print "Press 2 To Add New Pupils"
    print "Press 3 To Remove Pupils"
    print "Press 0 To Quit \n"

filename = open('pupil.txt','r')

pupil = ["James Steele", "Blain Krontick", "Leeroy Jenkins", "Tanvir Choudrey"]

def see_list(x):
    print x

def add_pupil(x):
    print "You have chosen to add a new pupil.\n"
    option = raw_input("Please type the childs name.")
    x.append(option)
    filename = open('pupil.txt','w') 
    filename.write('\n'.join(pupil)) 
    filename.close() 
    print option, "has been added to the system."
    return x

def delete_pupil(x):
    print "You have chosen to remove a pupil.\n"
    option = raw_input("Please type the childs name.")
    if option in x:
        x.remove(option)
        filename = open('pupil.txt','w') 
        filename.write('\n'.join(pupil)) 
        filename.close() 
        print option, "has been removed from the system."
    else:
        print "That person is not in the system."
    return x


one = 1
while one != 0:
    menu()
    option = input() 
    if option == 1:
        see_list(pupil)
    elif option == 2:
        add_pupil(pupil)
    elif option == 3:
        delete_pupil(pupil)
    elif option == 0:
        break
    else:
        print "That is not a valible choice."

filename = open('pupil.txt','w') 
filename.write('\n'.join(pupil)) 
filename.close() 


if option == 0:
    quit
4
  • 2
    Have a look at pickle module Commented May 18, 2012 at 19:47
  • Or json, for that matter. It's just a list. Commented May 18, 2012 at 19:50
  • By the way, if you want feedback on the code, don't be afraid to use codereview.stackexchange.com Commented May 18, 2012 at 19:51
  • input() in Python 2 calls eval() which introduce a security issue in your code. If you want numbers consider using int(raw_input()). Commented May 18, 2012 at 19:54

3 Answers 3

4

Well, you just open the pupil.txt file but never read back its contents. You need something like this:

filename = open('pupil.txt', 'r')
contents = filename.read()
filename.close()

pupil = [name for name in contents.split('\n') if name]

Also, you will need to handle the case when the pupil.txt file does not exist; this can be done with a try..except block around the IO calls.

Finally, as one of the comments has mentioned above, have a look at the pickle module, which lets you store a Python object in a file in Python's internal format (which is not really readable, but saves you a lot of hassle).

Sign up to request clarification or add additional context in comments.

2 Comments

Or, if it's simple data types (list, dict, etc), you can serialize it as JSON, which is both fast, and (arguably) human-readable.
Ok, thanks, I will look into both pickle and JSON, thanks for the code suggestion.
1

Not related to your question directly, but this:

one = 1
while one != 0:
    ...

is silly. All you need is:

while True:
    ...

2 Comments

for some reason it does not work for me, is it built in or do you have to define/ assign it.
Yes, it is built in. It's the constant for "true". The capitalization is important: it's True, not true.
0

This is what a database is for. Use sqlite - a simple file-based database the libraries for which come bundled with python.

2 Comments

Maybe once the application gets more complex, sure... But currently, I think that would be overkill. Since he's new to programming, he probably isn't familiar with SQL anyway.
voithos is correct I am do not know what SQL is but I will check it out once I have experieence in python!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.