dimanche 26 juillet 2015

Python exec function in an if statement

I would like to execute a text file as python script like so:

Text:

Vowel = a

Python:

text_file = open("C:\\Users\\user\\Desktop\\file.txt", "r")
lines = text_file.readlines()
for line in lines:
    variable = exec(line, globals(), locals())
    print(variable)

print(Vowel)

When I do this I get the desired result, however when it is in an if statement or a function, the exec function does not work. Is there a solution?

The if statement looks like this:

if(user_answer == True)
    text_file = open("C:\\Users\\user\\Desktop\\file.txt", "r")
    lines = text_file.readlines()
    for line in lines:
       variable = exec(line, globals(), locals())
        print(variable)
   print(Vowel)

Aucun commentaire:

Enregistrer un commentaire