DEV Community

Cover image for how to use shlex in python with example
saim
saim

Posted on • Originally published at pleypot.com

how to use shlex in python with example

In Python, the shlex module provides a convenient way to split shell-like syntax into tokens. This can be particularly useful when dealing with command-line inputs or parsing configuration files. By using shlex, you can ensure that your program correctly handles whitespace, quotes, and escape characters. For example, suppose you have a string containing a command-line-like input: command = "ls -l /usr/bin". Using shlex.split(), you can effortlessly split this string into tokens: tokens = shlex.split(command). This results in a list of tokens ['ls', '-l', '/usr/bin'], ready for further processing or execution within your Python script.


To illustrate further, consider a scenario where you want to execute a command with arguments stored in a string variable. Using shlex, you can easily parse this string into separate elements.

import shlex

command = "grep -i 'hello world' file.txt"
tokens = shlex.split(command)

print("Command:", tokens[0])
print("Arguments:", tokens[1:])
Enter fullscreen mode Exit fullscreen mode

shlex in python with example
The shlex.split() function splits the command string into individual elements, preserving quotes and spaces as expected. This makes it a valuable tool for handling complex command-line inputs within Python scripts.

Example 1

Shell Scripting in Python:

import shlex
import subprocess

command = "ls -l | grep 'example'"
command_tokens = shlex.split(command)
result = subprocess.run(command_tokens, capture_output=True, text=True)
Enter fullscreen mode Exit fullscreen mode

The shlex.split() is used to break down a shell command string (command) into individual elements, which are then executed using subprocess.run(). This approach enables Python scripts to execute shell commands safely and efficiently, handling complex command-line operations with ease.

Example 2

Configuring Docker Containers:
The shlex.split() is utilized to parse a command string intended for execution within a Docker container. By splitting the command accurately, Python scripts can configure and interact with Docker containers seamlessly, ensuring proper handling of arguments and preserving quoted strings as expected.

import shlex
import docker

client = docker.from_env()
container = client.containers.run("ubuntu", detach=True, command=shlex.split("echo 'Hello, Docker!'"))
Enter fullscreen mode Exit fullscreen mode

Example 3

Automating System Administration Tasks:
shlex.split() is employed to tokenize a series of shell commands (command) for execution using subprocess.run(). This approach is commonly used in system administration scripts to automate tasks such as file permission changes (chmod) and execution of custom scripts (./script.sh), streamlining system maintenance and management processes.

import shlex
import subprocess

command = "chmod +x script.sh && ./script.sh"
command_tokens = shlex.split(command)
subprocess.run(command_tokens)
Enter fullscreen mode Exit fullscreen mode

Read Also

πŸ“how to use python shlex subprocess with example
πŸ“how to use python shlex quote with example
πŸ“How to Create Pie Chart in Python with Pandas

Top comments (0)