Python Glossary Русский

all Python 3shlexterm

shlex

module

Parse and quote strings using Unix shell syntax: split a command line, quote an argument safely.

Syntax

import shlex

shlex.split breaks a command string into arguments while honouring quotes and escapes — str.split would cut "my file.txt" in two. shlex.quote and shlex.join go the other way, when a command still has to be built as a shell string. The rules are those of a POSIX shell, not Windows cmd.exe; better still, pass subprocess a list of arguments and skip shell=True.

Examples

import shlex
print(shlex.split('cp "my file.txt" backup/'))  # → ['cp', 'my file.txt', 'backup/']
print(shlex.join(['echo', 'hello world']))  # → echo 'hello world'
print(shlex.quote("it's"))  # → 'it'"'"'s'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy