How to store data from a text file in your code?

If I have this code for example:

import turtle, sys

def init_turtle():
    turtle.bgcolor('black')
    turtle.pencolor('yellow')
    turtle.fillcolor('dark sea green')
    turtle.pensize(2)
    turtle.left(90)
    turtle.speed(7)
    turtle.clear()
    turtle.home()
    turtle.penup()


def circle(x, y, radie):
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(radie)
    turtle.end_fill()
    turtle.penup()

init_turtle()

userinput = input("Enter the file you want to open: ")
myfile = open(userinput)
info = myfile.readlines()
print (info)
myfile.close()

input()

If I have a txt file with this code circle(0, -100, 100) it will only output the code into the command window like a text.

I want the code to be called into the program if that makes sense.

That way I can write different codes for multiple figures and store them in a few txt files. Then I just need to open my python program and input whichever txt file I want to open depending on the figure I want to be drawn.

I know I can use the if command combined with the input command and then write the different codes for the different figures directly into the program code but I want to do it with the method I described above.

Hope you guys understand what I want to be done, I have searched for hours but can’t find a tutorial or any command on what I want to be done. Maybe I’m just using the wrong keywords when searching but yeah…thanks if you can help me!

I think CC does a lesson on reading/writing files. This is the one from Python 2, although I think the two versions ar every similar.

1 Like