Mark Costello
Mark Costello

Reputation: 155

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,
                          'ID':member_ID,
                          'Job':member_job}}
    strdicto = str(dicto)
    f.write(strdicto)
    menu()

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

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

menu()

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.

Upvotes: 0

Views: 503

Answers (3)

Václav Struhár
Václav Struhár

Reputation: 1769

  • Use f.flush() when you want to force your program to write into the file.
  • Always use f.close() when you are done with reading / writing your file.

Upvotes: 0

Peter Steele
Peter Steele

Reputation: 224

When I run your code with a file named memberdatabase.txt in the same folder as the py script it has no issues saving the text to the file. You just have to make sure to close the file when you are done. This is what it should look like:

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,
                          'ID':member_ID,
                          'Job':member_job}}
strdicto = str(dicto)
f.write(strdicto)
f.close()

This is the output I get in the file after adding two members:;

{'Peter': {'Age': 26, 'ID': 21, 'Job': 'Manager'}}{'Bob': {'Age': 34, 'ID': 1209, 'Job': 'Manager'}}

I was able to have the file closed and then opened again and the text was still there. Hope this helps so you can implement it in your code now.

Upvotes: 0

Boldewyn
Boldewyn

Reputation: 82724

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.

Upvotes: 1

Related Questions