Question

test1.py is the main script calling another script test2.py by passing the same argument list that has been passed to test1.py. I have done following but it reads the sys.argv list as string and parse into multiple arguments and also includes unnecessary [ and ,

test1.py
import os
import sys

argList=sys.argv[1:]

os.system('python another/location/test2.py %s'%(argList))

test2.py

import sys
print(sys.argv[1:])

Call test1.py
python test1.py -a -b -c
output: ['[-a,' ,'-b,', '-c]' ]

Please post if there is a better opti

Was it helpful?

Solution

Use

os.system('python another/location/test2.py %s' % ' '.join(argList))

if the arguments will contain no spaces themselves.

The second program will output

['-a', '-b', '-c']

If your arguments can contain spaces, it might be best to quote them. Use ' '.join("'%s'" % arg.replace("'", "\\'") for arg in ArgList)

OTHER TIPS

in the case if you need to exit test1.py just after calling test2.py

import subprocess
subprocess.Popen( ('python', 'another/location/test2.py') + tuple( sys.argv[1:]) )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top