Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:48:59+00:00 2026-06-04T03:48:59+00:00

I am new to python (and programming in general) and am making a database/register

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:49:00+00:00Added an answer on June 4, 2026 at 3:49 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to Python and programming in general, and I was wondering if
New to Python and programming in general. I want to install a module from
I'm very new to Python and multithreaded programming in general. Basically, I have a
I am new to Python, and programming in general. While I think this question
I'm new to Python and programming in general (a couple of weeks at most).
I'm fairly new to Python and programming in general. I have done a few
I am somewhat new to Python and programming in general so I apologize. By
I'm new to Squeak and Smalltalk but not programming in general(i've some Python experience).
So basically im new to python and programming in general. I was wondering say
So basically im new to python and programming in general. I was wondering say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.