Running scripts within scripts
Brandon Beck
bbeck at REMOVE-THIS-TO-EMAIL-ME.austin.rr.com
Tue Jul 2 23:23:19 EDT 2002
Paul Rubin wrote:
> "Todd" <todd_gable at invision.iip.com> writes:
>
>>Is it posible for a python to call a second without importing a module? I
>>don't really want to call os.system and have to include paths. Just call a
>>second script in the same directory as the parent one.
>
>
> You could say
>
> os.system("./other-script.py")
>
> which doesn't need to include any paths.
Seems like this is exactly what execfile was written for. It does
require a file object to work on though but it's not hard to get one
try:
# use open() to get a file object. In python 2.2 and
# later, use file() instead.
fin = open('other-script.py')
except IOError:
# print witty error message here
# execute the code in the specified file
execfile(fin)
There are more advanced versions of execfile that take a globals and
locals maps. See the python documentation at http://www.python.org for
instructions on how to use them.
Hope this helps,
Brandon
More information about the Python-list
mailing list