shlex非常に便利

雑にシェルスクリプトlikeな構文利用したい時に使える。

>>> import shlex
>>> shlex.split("""foo bar "foo bar" 'foo bar' test""")
['foo', 'bar', 'foo bar', 'foo bar', 'test']
>>> shlex.split("""foo bar "foo bar" 'foo bar' test""", posix=False)
['foo', 'bar', '"foo bar"', "'foo bar'", 'test']
>>> shlex.split("""line1\n#comment\nline2""")
['line1', '#comment', 'line2']
>>> shlex.split("""line1\n#comment\nline2""", comments=True)
['line1', 'line2']

参考