line to argv transformation
Chris Angelico
rosuav at gmail.com
Mon Jun 16 06:41:21 EDT 2014
On Mon, Jun 16, 2014 at 8:24 PM, Antoon Pardon
<antoon.pardon at rece.vub.ac.be> wrote:
> On 16-06-14 12:06, Chris Angelico wrote:
>
>> def shell_split(cmd):
>> return subprocess.check_output("""python -c 'import sys;
>> print("\\0".join(sys.argv[1:]))' """+cmd,shell=True)[:-1].split("\0")
>
> Nice idea, unfortunatly it doesn't work in python3.3
>
>>>> shell_split("ls *.py")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 3, in shell_split
> TypeError: Type str doesn't support the buffer API
>>>>
Oops! I made the cardinal error of trying in one and assuming it'd
work in both. Just needs a b prefix on the split string:
def shell_split(cmd):
return subprocess.check_output("""python -c 'import sys;
print("\\0".join(sys.argv[1:]))' """+cmd,shell=True)[:-1].split(b"\0")
You'll get back a list of byte strings, in any case. Feel free to pass
them through a decode operation, or to incorporate a .decode() into
the above stream, as you wish.
ChrisA
More information about the Python-list
mailing list