Nik Kantar

Monday, November 27, 2023
1 min read

TIL About shlex.split

Today I learned about shlex.split and it’s really cool.

Let’s say we have a string in Python, for example a command with some arguments, like so:

cmd = 'pytest -m "not live"'

If you want to split it into pieces, you’d use str.split, like so:

>>> cmd.split()
['pytest', '-m', '"not', 'live"']

But that doesn’t quite get it right, with "not live" split into "not and live". Sure, it’s technically correct, which we all know is the best kind of correct, but we probably want those two parts to go together.

Enter shlex.split:

>>> import shlex
>>> shlex.split(cmd)
['pytest', '-m', 'not live']

shlex is a wicked cool built-in module for lexical analysis of Unix shells.

Hat tip to Brian Okken and his podcast episode and blog post about argparse testing.


Tags: programming, python

Thanks for reading! You can keep up with my writing via the feed or newsletter, or you can get in touch via email or Mastodon.


Older:
Muscle Memory Is Wild
Newer:
Nik’s Issue Tracker