One step up from str.split()

Matimus mccredie at
Tue Jul 15 03:56:34 CEST 2008

On Jul 14, 6:33 pm, "Joel Koltner" <zapwireDASHgro... at>
> I normally use str.split() for simple splitting of command line arguments, but
> I would like to support, e.g., long file names which-- under windows -- are
> typically provided as simple quoted string.  E.g.,
> myapp --dosomething --loadthis "my file name.fil"
> ...and I'd like to get back a list wherein ListEntry[3]="my file name.fil" ,
> but just running str.split() on the above string creates:
> >>> ListEntry='myapp --dosomething --loadthis "my file name.fil"'
> >>> ListEntry.split()
> ['myapp', '--dosomething', '--loadthis', '"my', 'file', 'name.fil"']
> Is there an easy way to provide just this one small additional feature
> (keeping quoted names as a single entry) rather than going to a full-blown
> command-line argument parsing tool?  Even regular expressions seem like they'd
> probably be overkill here?  Or no?
> Thanks,
> ---Joel

look at the shlex module:

>>> import shlex
>>> txt = 'myapp --dosomething --loadthis "my file name.fil"'
>>> shlex.split(txt)
['myapp', '--dosomething', '--loadthis', 'my file name.fil']


More information about the Python-list mailing list