Search code examples
pythonsubprocesssys

Python program to execute any other Python program as argument?


I am trying to write a Python program to to execute another Python program using subprocess. What wrong in this program, and how can I take the other Python program as an argument?

import sys
import subprocess
def dorun(args):
   subprocess.Popen([sys.executable, '%r'] % args)
dorun()

The error is:

najeeb@najeeb:~/Desktop/project$ python new-test.py nmap-test.py 
Traceback (most recent call last):
File "new-test.py", line 9, in <module>
dorun()
TypeError: dorun() takes exactly 1 argument (0 given)

Solution

  • Unless you want to be able to launch several program at one ?

    import sys
    import subprocess
    def dorun(args):
       subprocess.Popen([sys.executable, args])
    dorun(sys.argv[1])