Python program to execute any other Python program as argument?

Question:

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)
Asked By: Najeeb Choudhary

||

Answers:

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])
Answered By: Arkantus
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.