Mark Costello Mark Costello -4 years ago 89
Python Question

Python Program won't write in a text file

So, currently I'm writing a "Member Database" program for a client. The program needs to be able to put all inputted info into a text file that can be read at any time. Currently, I'm working on a prototype-type thing, that currently is not indicative of the final product.

import os

def add_member():
f = open("memberdatabase.txt","a+")
member_name = input('Input Member Name\n')
member_age = int(input('Input Member Age\n'))
member_ID = int(input('Input Member ID\n'))
member_job = input('Input Member Job\n')

dicto = {member_name:{'Age':member_age,
strdicto = str(dicto)

def read_data():
f = open("memberdatabase.txt","r")
contents =
input('Press enter to return to menu')

def menu():
x = int(input('Enter an option\n'
'[1] Add a member\n'
'[2] Read Data\n'))
if x == 1:
elif x == 2:
print('Not an option, retry!')


After the user has finished inputting the information, it needs to be written and then return to the menu. Currently, it just won't write into the text file.

Answer Source

You never f.close() the file. You could use auto-closing with the with statement:

with open('...', 'a+') as f:
    # f.close() is called automatically at the end of this block.

By the way: same with reading it: Put the reading in a with open() as f block.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download