So I am trying to make a python program to write some code to itself the run the code it created before the session ends, like this:
for i in range(10):
with open('test.py','a') as f:
f.writelines(' print("Hello World!")\n')
So, the 3rd line creates a 4th line which would print 'Hello World!', next iteration it would print it twice (because then it would have created yet another line saying the same thing) all the way up to 10 iterations. So, in the end, it looks like this:
for i in range(10):
with open('test.py','a') as f:
f.writelines(' print("Hello World!")\n')
print("Hello World!")
print("Hello World!")
print("Hello World!")
... Up to 10
(However, I mainly want to store outputted data into a variable from this, not prints or anything along those lines).
The problem is that it doesn't update fast enough. When you run the program for the first time you get nothing, then if you close and reopen the code you see all ten 'print('Hello World!')'s. I have no clue how to solve this...
Thanks!