1

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

1
  • When you do the string formatting, what is argList converted into? It's not quite what you expect. You want some way to format argList yourself...look into the join method of a string. Commented Oct 9, 2013 at 22:38

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, what is the use of join() ? will it append all List element with the separator ' '(space) ?
Indeed. string.join(list) will do list[0] + string + list[1] + ... (but it's better).
0

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:]) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.