Shani de Leeuw Shani de Leeuw - 6 months ago 11
Python Question

Using $* as part of a parameter for os.system in python

The command:

make foo -f $*

Has different functionality when called from the command line versus when it is called from a python script as follows:

import os
os.system(make foo -f $*)

As stated here:
$* in a bat file is basically all the positional parameters seen as a single word.

Python seems to be parsing it as simply "$*". Is there anyway to get around this and replicate the same functionality?

I realise I can write a .bat script and call that with python but I was hoping for something more eloquent.


As you point out, $* has no special meaning in python. The comprehension is done entirely by whatever shell you are using. If you want to pass all positional parameters passed to your script to some command, then you can try the following

import os, sys
os.system("make foo -f {}".format(" ".join(sys.argv[1:])))

Please note however that os.system is deprecated. You should probably use

import subprocess, sys
subprocess.check_call("make foo -f {}".format(" ".join(sys.argv[1:])), shell=True)



As suggested in the comments, one should avoid using shell=True whenever the command is built from "untrusted" input, such as the command line of the program. Therefore a much better alternative is to use

import subprocess, sys
subprocess.check_call(['make', 'foo', '-f'] + sys.argv[1:])