Call script which accepts com. line par. from another scriptand error control

Peter Otten __peter__ at web.de
Thu May 24 04:47:38 EDT 2007


Karim Ali wrote:

> What I still dont know though is how do I handle the fact that the first
> script is expecting command line parameters. I would like to be able to
> replace the command line parameters by a variable such that the second
> script can call: first_script.main("command line"). Is this possible?

I think it is better to pass a list of arguments

# first_script.py 

def main(args=None):
    parser = optparse.OptionParser()
    # add options
    options, positional_args = parser.parse_args(args) 
    # process

if __name__ == "__main__":
    # will read options from the command line
    # if module is invoked as a standalone script
    main() 

# second_script.py

import first_script
first_script.main(["-f", "--bar", "what", "you want"])

That way you don't have to deal with escaping spaces etc.

Peter




More information about the Python-list mailing list