line to argv transformation

Chris Angelico rosuav at gmail.com
Mon Jun 16 06:06:19 EDT 2014


On Mon, Jun 16, 2014 at 7:56 PM, Antoon Pardon
<antoon.pardon at rece.vub.ac.be> wrote:
> I am looking for an interface that takes a string as argument. The
> string is to be treated as if it is a command line and transformed into
> an argv list.
>
> "ls file"   -> ['ls', 'file']
> "ls *.py"   -> ['ls', 'file1.py', 'file2.py', ...]
> "ls '*.py'" -> ['ls', '*.py']
>
> Does something like this already exist? I looked around but seem to find
> only things only partially do things like this, like shlex.split.

def shell_split(cmd):
    return subprocess.check_output("""python -c 'import sys;
print("\\0".join(sys.argv[1:]))' """+cmd,shell=True)[:-1].split("\0")

:)

ChrisA



More information about the Python-list mailing list