Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96118

Re: passing double quotes in subprocess

Path csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'one?': 0.05; 'escape': 0.07; 'cc:addr:python-list': 0.09; 'literal': 0.09; 'python': 0.10; 'backslashes': 0.16; 'commandline': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'googled': 0.16; 'subprocess': 0.16; 'wrote:': 0.16; 'string': 0.17; 'shell': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fix': 0.21; 'fine,': 0.22; 'sep': 0.22; 'header:In-Reply-To:1': 0.24; 'script': 0.25; "doesn't": 0.26; 'appear': 0.26; 'command': 0.26; 'external': 0.27; 'parameters': 0.27; 'raw': 0.27; 'message-id:@mail.gmail.com': 0.27; 'print': 0.30; 'another': 0.32; 'getting': 0.33; 'quotes': 0.33; "skip:' 20": 0.34; 'tue,': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:p 20': 0.38; 'your': 0.60; 'provide': 0.61; 'better.': 0.66; 'chrisa': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=DqF08r65X7iYg66e2Ldw2zL+m/7FsDkMGzJYiaUHWGY=; b=aOdIXdE8+/31PVuB+7ALUVK6VusVFXc+BKSXJiXGaHXA+izXaEogIu9EzzdLB21vNp HUtQJYXQhPMP0KJ/YHq4Sqyab034aDVfEbam67Uk9ro7RpfDUBi2XgxXUKl2paXb/HyK uA1li57u3U1Sjgx78upcKiG0UWTJt3B01YezdOp3ynSWibC/6uXrxZSl0bd+H4Igvt0x bjPju5y8+yltl21/4CKVR2udcCHzoAwpNN7fWgIZHFzd0m+c9jGBdNF3pDRWfMgIDkQP k8cksfOPMQZdv0usINkxMQOPsbxlzDIjXfF+JyPtrUfgOhAoMtLKH9guOrMRMmt85r78 4Gig==
MIME-Version 1.0
X-Received by 10.50.98.7 with SMTP id ee7mr40013146igb.13.1441710812214; Tue, 08 Sep 2015 04:13:32 -0700 (PDT)
In-Reply-To <1e6570d1-42be-432d-a690-43c4e417ec00@googlegroups.com>
References <1e6570d1-42be-432d-a690-43c4e417ec00@googlegroups.com>
Date Tue, 8 Sep 2015 21:13:32 +1000
Subject Re: passing double quotes in subprocess
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.214.1441710815.8327.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1441710815 news.xs4all.nl 23756 [2001:888:2000:d::a6]:60529
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:96118

Show key headers only | View raw


On Tue, Sep 8, 2015 at 9:03 PM, loial <jldunn2000@gmail.com> wrote:
> I need to execute an external shell script via subprocess on Linux.
>
> One of the parameters needs to be passed inside double quotes
>
> But the double quotes do not appear to be passed to the script
>
> I am using :
>
> myscript = '/home/john/myscript'
> commandline = myscript + ' ' + '\"Hello\"'
>
> process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> output,err = process.communicate()
>
>
> if I make the call from another shell script and escape the double quotes it works fine, but not when I use python and subprocess.
>
> I have googled this but cannot find a solution...is there one?

First off: Can you remove the shell=True and provide the command as a
list, so it doesn't need to be parsed? If you can, that would be MUCH
better.

But if you can't, the simple fix is to use a raw string literal for
your Hello. The backslashes are getting lost:

>>> print('\"Hello\"')
"Hello"
>>> print(r'\"Hello\"')
\"Hello\"

If In Doubt, Print It Out. It's amazing how much you can learn with a
few well-placed print() calls :)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

passing double quotes in subprocess loial <jldunn2000@gmail.com> - 2015-09-08 04:03 -0700
  Re: passing double quotes in subprocess Larry Martell <larry.martell@gmail.com> - 2015-09-08 07:11 -0400
  Re: passing double quotes in subprocess Chris Angelico <rosuav@gmail.com> - 2015-09-08 21:13 +1000
  Re: passing double quotes in subprocess loial <jldunn2000@gmail.com> - 2015-09-08 04:13 -0700
  Re: passing double quotes in subprocess Akira Li <4kir4.1i@gmail.com> - 2015-09-08 14:15 +0300

csiph-web