bjskistad bjskistad - 1 year ago 96
Python Question

How to execute file in Python with arguments using import?

I read that you can execute a file using

like this

#!/usr/bin/env python
import file2

#!/usr/bin/env python
print "Hello World!"

And will print
Hello World
. How would I execute this file with arguments, using

Answer Source

Program arguments are available in sys.argv and can be used by any module. You could change to

import sys
print "Here are my arguments", sys.argv

You can use the argparse module for more complex parsing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download