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


Groups > comp.lang.python > #96120

Re: passing double quotes in subprocess

Path csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'one?': 0.05; 'python3': 0.05; 'escape': 0.07; 'cmd': 0.09; 'output,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; '\'"\'': 0.16; 'commandline': 0.16; 'googled': 0.16; 'literals:': 0.16; 'popen,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subprocess': 0.16; 'shell': 0.18; '>>>': 0.20; 'fine,': 0.22; 'pipe': 0.22; 'errors': 0.23; 'import': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'header:X-Complaints-To:1': 0.26; 'external': 0.27; 'parameters': 0.27; 'skip:# 10': 0.27; 'preserve': 0.29; 'another': 0.32; 'quotes': 0.33; 'message-id:@gmail.com': 0.34; "skip:' 20": 0.34; 'but': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'here:': 0.63; 'received:89': 0.80
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Akira Li <4kir4.1i@gmail.com>
Subject Re: passing double quotes in subprocess
Date Tue, 08 Sep 2015 14:15:55 +0300
References <1e6570d1-42be-432d-a690-43c4e417ec00@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain
X-Gmane-NNTP-Posting-Host 89.169.229.68
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:zPkkJjLXdTLZAcov0y5UfpHQ9+Q=
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.215.1441711041.8327.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1441711041 news.xs4all.nl 23859 [2001:888:2000:d::a6]:36873
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:96120

Show key headers only | View raw


loial <jldunn2000@gmail.com> writes:

> 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?

You don't need shell=True here:

  #!/usr/bin/env python3
  from subprocess import Popen, PIPE

  cmd = ['/home/john/myscript', 'Hello'] # if myscript don't need quotes
  # cmd = ['/home/john/myscript', '"Hello"'] # if myscript does need quotes
  with Popen(cmd, stdout=PIPE, stderr=PIPE) as process:
     output, errors = process.communicate()

In general, to preserve backslashes, use raw-string literals:

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


  >>> '\"' == '"'
  True 
  >>> r'\"' == '\\"'
  True

Back to comp.lang.python | Previous | NextPrevious 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