I'm trying to use execfile() in a Python program, but I do not know why it works in one situation and not work in another. I'm really new to Python and so I do not have enough experience to figure this out. Plus I'm not a programmer.
In this situation, it does not work:
#file1.py
mynums=[1,2,3]
#file2.py
def mystuff():
<tab>execfile(' file1.py')
<tab>print mynums
mystuff()
This is giving me an error "mynums not defined"
In this situation it does work:
#file3.py
execfile('file1 .py')
print mynums
>>> [1,2,3]
I'm using Python 2.7. All files are in the same folder.
In this situation, it does not work:
#file1.py
mynums=[1,2,3]
#file2.py
def mystuff():
<tab>execfile(' file1.py')
<tab>print mynums
mystuff()
This is giving me an error "mynums not defined"
In this situation it does work:
#file3.py
execfile('file1 .py')
print mynums
>>> [1,2,3]
I'm using Python 2.7. All files are in the same folder.
Comment