Python Shlex Module
The shlex module makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This is often used for splitting command strings for subprocess.
Importing the Module
import shlex
Splitting Strings
The shlex.split() function splits a string into a list of tokens, using shell-like syntax. This is useful when you want to process a command line string.
import shlex
command_line = 'ls -l "some file with spaces.txt"'
args = shlex.split(command_line)
print(args)
# Output: ['ls', '-l', 'some file with spaces.txt']
This is safer and more correct than using string.split() because it handles quoted strings correctly.
Quoting Strings
The shlex.quote() function returns a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line.
import shlex
filename = 'some file; rm -rf /'
command = f"ls -l {shlex.quote(filename)}"
print(command)
# Output: ls -l 'some file; rm -rf /'
Joining Tokens
The shlex.join() function concatenates the tokens of the list split_command and returns a string. This is the inverse of split().
Note: Added in Python 3.8.
import shlex
args = ['echo', 'Hello', 'World!']
command = shlex.join(args)
print(command)
# Output: echo Hello 'World!'