[Python] Python File Keeps Running Previous Output

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing an interpreter that can understand English commands, for example, 'say Hello World.' The Python code reads input from stdin. I have no problem with that. Then it translates it into Python code and appends it to a file. Again, no problem. Then I have a function that runs the code in the file. The function goes like this:

    def run(runfile):
    with open(runfile,"r") as rnf:
    exec(rnf.read())


    Then I just write:

    run("file.py")


    I then run the code in the file and simulate the output. Then I open the file as if to write to it and then close it at the end of the program (on the last line) to delete the file's contents like this:

    open("file.py", "w").close()


    The problem is that when I type in English commands and run the file, it runs properly. Then I type in a different English command, and it runs the output from the previous execution. Then I run it again and it works properly. Why does this happen and how can I fix it? (Feel free to ask questions about my code)

    Continue reading...

Compartilhe esta Página