File Access in python
<p>Whenever I go to load a text file for my program it displays the info in the text file yet when I input the roster function it does not display the text file and show it is available to be modified. Is it something with how I created the text file in the first place or is my coding for <code>loadData</code> not written correctly i think i may be confused on the difference between just setting up a function just to read back the file instead of actually opening the text file to be able to modify it.</p>

<pre><code>dict_member = {}
players = dict_member
class Players:
    def __init__(self, name, number, jersey):
        self.name = name
        self.number = number
        self.jersey = jersey
    def display(self):
        print('Printing current members\n')
        for number, player in dict_member.items():
            print(player.name + ', ' + player.number + ', ' + player.jersey)
    def add(self):
        nam = input("Enter Player Name\n ")
        numb = input("Enter Player Number\n ")
        jers = input("Enter Jersey Number\n ")
        dict_member[nam] = Players(nam, numb, jers)
    def remove(self, name):
        if name in dict_member:
            del dict_member[name]
    def edit(self, name):
        if name in dict_member:
            nam = input("Enter Different Name\n")
            num = input("Enter New Number\n ")
            jers = input("Enter New Jersey Number\n ")
            del dict_member[name]
            dict_member[name] = Players(nam, num, jers)
        else:
            print("No such player exists")

    def saveData(self):
        roster = input("Filename to save: ")
        print("Saving data...")
        with open(roster, "r+") as rstr:
            for number, player in dict_member.items():
                rstr.write(player.name + ', ' + player.number + ', ' + player.jersey)
            print("Data saved.")
            rstr.close()

    def loadData(self):
        dict_member = {}
        roster = input("Filename to load: ")
        file = open(roster, "r")
        while True:
            inLine = file.readline()
            if not inLine:
                'break'
            inLine = inLine[:-1]
            name, number, jersey = inLine.split(",")
            dict_member[name] = (name, number, jersey)
        print("Data Loaded Successfully.")
        file.close()
        return dict_member

def display_menu():
    print("")
    print("1. Roster ")
    print("2. Add")
    print("3. Remove ")
    print("4. Edit ")
    print("5. Save")
    print("6. Load")
    print("9. Exit ")
    print("")
    return int(input("Selection&gt; "))
print("Welcome to the Team Manager")
player_instance = Players(None, None, None)
menu_item = display_menu()
while menu_item != 9:
    if menu_item == 1:
        player_instance.display()
    elif menu_item == 2:
        player_instance.add()
    elif menu_item == 3:
        m = input("Enter Player to Remove\n")
        player_instance.remove(m)
    elif menu_item == 4:
        m = input("Enter Player to Edit\n")
        player_instance.edit(m)
    elif menu_item == 5:
        player_instance.saveData()
    elif menu_item == 6:
        player_instance.loadData()
    menu_item = display_menu()
print("Exiting Program...")
</code></pre>

<p>you didnt load your data into <code>dict_member</code> before displaying the roster </p>

<p>when you load your data in the loadData function you redefine <code>dict_member</code>
so it will "shadow" the outer <code>dict_member</code> so when the <code>display</code> function is called <code>dict_member</code> will always be empty</p>

<p>so you probably want to remove this line </p>

<pre><code>def loadData(self):
    #  **remove this line ---&gt;** dict_member = {}
    roster = input("Filename to load: ")
    file = open(roster, "r")
    while True:
        inLine = file.readline()
        if not inLine:
            break
        inLine = inLine[:-1]
        name, number, jersey = inLine.split(",")
        dict_member[name] = (name, number, jersey)
    print("Data Loaded Successfully.")
    file.close()
    return dict_member
</code></pre>

python