I'm trying to write a program that can store the contents of a file into a variable chosen by the user. For example, the user would choose a file located in the current directory and would then choose the variable they wanted to store it in. This is a segment of my code so far.
print("What .antonio file do you want to load?")
loadfile = input("")
open(loadfile, "r")
print("Which variable do you want to load it for? - list_1, list_2, list_3")
whichvariable = input("")
if whichvariable == "list_1":
list_1 = loadfile.read()
elif whichvariable == "list_2":
list_2 = loadfile.read()
elif whichvariable == "list_3":
list_3 = loadfile.read()
else:
print("ERROR")
When I input that loadfile = list1.antonio
(which is an existing file) and whichvariable = list_1
it throws me this error:
Traceback (most recent call last):
File "D:\Antonio\Projetos\Python\hello.py", line 29, in <module>
list_1 = loadfile.read()
AttributeError: 'str' object has no attribute 'read'
I have tried all sorts of things and I haven't find a solution.