The exec() function is not executing a string correctly.
def servlistappend(x):
file = open("blarg.txt",'r')
read = str(file.read())
print(read)
tbe = "list = "+read
print(tbe)
exec(tbe)
print(list)
list.append(x)
print(list)
file.close()
file = open("blarg.txt",'w')
file.write(str(list))
file.close()
def servlistremove(x):
file = open("blarg.txt",'r')
read = str(file.read())
print(read)
tbe = "list = "+read
print(tbe)
exec(tbe)
print(list)
if x in list:
list.remove(x)
else:
print("that element does not exist")
print(list)
file.close()
file = open("blarg.txt",'w')
file.write(str(list))
file.close()
servlistremove(2345)
The contents of blarg.txt are: [1234,2345,2345,2345]
When run, print(read) correctly outputs the string [1234,2345,2345,2345]
and print(tbe) correctly outputs the stringlist = [1234, 2345, 2345, 2345]
. After that, exec(tbe) and print(list) outputs <class 'list'>
while it is instead supposed to output the list [1234,2345,2345,2345]
. Does anyone know why this happens?