Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

execfile and argv

1,483 views
Skip to first unread message

Chris Berthold

unread,
Jun 3, 1997, 3:00:00 AM6/3/97
to

Is it possible to execute a file which requires arguments using
"execfile"? I.e. if the usage of file "test.py" is normally

>>> test -a pathname

then can I use execfile to run test.py with these arguments. If so, what
syntax do I use?

Thank you

Terry Reedy

unread,
Jun 5, 1997, 3:00:00 AM6/5/97
to

In article <339460...@ntr.net>, chri...@ntr.net says...

Reference manual says:
"execfile (file[, globals[, locals]])
This function is similar to the exec statement, but parses a file
instead of a string. It is different from the import statement in that
it does not use the module administration --- it reads the file
unconditionally and does not create a new module. (1)

The arguments are a file name and two optional dictionaries. The file is
parsed and evaluated as a sequence of Python statements (similarly to a
module) using the globals
and locals dictionaries as global and local name space. If the locals
dictionary is omitted it defaults to the globals dictionary. If both
dictionaries are omitted, the expression is executed in the environment
where execfile() is called. The return value is None."

which does not answer your question.

Wild guess:

import sys
sys.argv = ['test', '-a', 'pathname']
execfile('test')

will do what you want. Of course, you had better copy or extract any
info you need from original argv first.

TJ Reedy

0 new messages