Python subprocess print command with arguments

Using the command string, parse command into a list of arguments.

$ python
>>> import shlex
>>> cmd = '/usr/bin/binary -v --recursive --exclude=".git"'
>>> shlex.split(cmd)
['/usr/bin/binary', '-v', '--recursive', '--exclude=.git']

Using a list of arguments, reconstruct the command executed into a string.

$ python
>>> import subprocess
>>> cmd_list = ['/usr/bin/binary', '-v', '--recursive', '--exclude=.git']
>>> subprocess.list2cmdline(cmd_list)
'/usr/bin/binary -v --recursive --exclude=.git'
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment