Search code examples
pythonstring-parsing

Is there a python standard function to parse a string into an argv list exactly like bash does, in Python?


Consider the following string:

./kmux.py  -r 'messenger.* xx'

If kmux.py is a python script, and the string above was given to bash as an argument, then sys.argv will be the following list:

["./kmux.py", "-r", "messenger.* xx"]

Is there a standard python function that will take the first string and turn it into the array?

I could manually replicate bash's logic in Python to take the original string and turn it into this array, but I am curious about whether such a function already exists.


Solution

  • Use the default shlex package like so:

    import shlex
    shlex.split("'./kmux.py  -r 'messenger.* xx'")