It depends on the version of Python you are using. In Python3.3, the arg is saved in proc.args
:
proc = subprocess.Popen(....)
print("the commandline is {}".format(proc.args))
In Python2.7, the args
not saved, it is just passed on to other functions like _execute_child
. So, in that case, the best way to get the command line is to save it when you have it:
proc = subprocess.Popen(shlex.split(cmd))
print "the commandline is %s" % cmd
Note that if you have the list of arguments (such as the type of thing returned by shlex.split(cmd)
, then you can recover the command-line string, cmd
using the undocumented function subprocess.list2cmdline
:
In [14]: import subprocess
In [15]: import shlex
In [16]: cmd = 'foo -a -b --bar baz'
In [17]: shlex.split(cmd)
Out[17]: ['foo', '-a', '-b', '--bar', 'baz']
In [18]: subprocess.list2cmdline(['foo', '-a', '-b', '--bar', 'baz'])
Out[19]: 'foo -a -b --bar baz'