Python 3.8+shlexfunction
shlex.join()
command-line parsing
Joins a list of arguments into a shell command string, escaping each one with shlex.quote(). The inverse of shlex.split().
Syntax
shlex.join(split_command)
Unlike ' '.join(args), it preserves arguments containing spaces and quotes: split(join(args)) gives back the original list. It is handy for printing or logging a command you pass to subprocess as a list, so it can be pasted into a terminal. The result is not valid for cmd.exe on Windows.
Examples
import shlex
args = ['grep', '-r', 'hello world', "it's"]
print(shlex.join(args)) # → grep -r 'hello world' 'it'"'"'s'
print(shlex.split(shlex.join(args)) == args) # → True
print(' '.join(args)) # → grep -r hello world it's — границы аргументов потеряны
See also
Open in the full glossarydocs.python.orgReport an inaccuracy