Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73310
| References | <539EBF61.4050106@rece.vub.ac.be> <CAPTjJmqd74v013WwG2QrZP2y9N+gMk0TK5xT5-yy3=8gOuAs4g@mail.gmail.com> <539EC5EF.7040205@rece.vub.ac.be> |
|---|---|
| Date | 2014-06-16 20:41 +1000 |
| Subject | Re: line to argv transformation |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11082.1402915284.18130.python-list@python.org> (permalink) |
On Mon, Jun 16, 2014 at 8:24 PM, Antoon Pardon
<antoon.pardon@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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: line to argv transformation Chris Angelico <rosuav@gmail.com> - 2014-06-16 20:41 +1000
Re: line to argv transformation Marko Rauhamaa <marko@pacujo.net> - 2014-06-16 13:59 +0300
Re: line to argv transformation Chris Angelico <rosuav@gmail.com> - 2014-06-16 21:25 +1000
csiph-web