Jason Ray Jason Ray - 1 month ago 14
Python Question

Loading txt file into python

Ok Ive added a save feature and it works correctly from the tutorials I read it has been stated that making a load function is not that much different from adding a save function my save feature writes to a txt file as it should yet my load function does not load the requested file. I know i went wrong somewhere and i apologize on asking so many questions. I am still somewhat new to python. Also not looking for someone to write the code for me just needing a better explanation then im obviously finding

Traceback (most recent call last):
File "C:/Users/Family/PycharmProjects/Week5/Team Management 2.py", line 75, in <module>
player_instance.loadData()
File "C:/Users/Family/PycharmProjects/Week5/Team Management 2.py", line 43, in loadData
rstr.load(player.name + ', ' + player.number + ', ' + player.jersey)
AttributeError: '_io.TextIOWrapper' object has no attribute 'load'



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, 'w') as rstr:
for number, player in dict_member.items():
rstr.write(player.name + ', ' + player.number + ', ' + player.jersey)
print("Data saved.")
rstr.closed

def loadData(self):
roster = input("Filename to load: ")
print("Loading data...")
with open(roster, 'rt') as rstr:
rstr.load(player.name + ', ' + player.number + ', ' + player.jersey)
print("Data loaded.")

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

Answer

Try replacing:

outFile = open(roster)
for name in dict_member():
    outFile.write(name + "," + number + "," + jersey + "," + "\n")
    print("Data saved.")
    outFile.close()

with:

with open(roster, 'w') as rstr:
    rstr.write(name + "," + number + "," + jersey + "," + "\n")
    print("Data saved.")