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


Groups > comp.lang.python > #96116 > unrolled thread

passing double quotes in subprocess

Started byloial <jldunn2000@gmail.com>
First post2015-09-08 04:03 -0700
Last post2015-09-08 14:15 +0300
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#96116 — passing double quotes in subprocess

Fromloial <jldunn2000@gmail.com>
Date2015-09-08 04:03 -0700
Subjectpassing double quotes in subprocess
Message-ID<1e6570d1-42be-432d-a690-43c4e417ec00@googlegroups.com>
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?

[toc] | [next] | [standalone]


#96117

FromLarry Martell <larry.martell@gmail.com>
Date2015-09-08 07:11 -0400
Message-ID<mailman.213.1441710675.8327.python-list@python.org>
In reply to#96116
On Tue, Sep 8, 2015 at 7:03 AM, 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?

Try it with 2 backslashes:

commandline = myscript + ' ' + '\\"Hello\\"'

[toc] | [prev] | [next] | [standalone]


#96118

FromChris Angelico <rosuav@gmail.com>
Date2015-09-08 21:13 +1000
Message-ID<mailman.214.1441710815.8327.python-list@python.org>
In reply to#96116
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

[toc] | [prev] | [next] | [standalone]


#96119

Fromloial <jldunn2000@gmail.com>
Date2015-09-08 04:13 -0700
Message-ID<bae8be33-152c-4994-bf17-ab6e3b93f64f@googlegroups.com>
In reply to#96116
Yep, that did the trick...cheers

On Tuesday, September 8, 2015 at 12:04:05 PM UTC+1, loial 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?

[toc] | [prev] | [next] | [standalone]


#96120

FromAkira Li <4kir4.1i@gmail.com>
Date2015-09-08 14:15 +0300
Message-ID<mailman.215.1441711041.8327.python-list@python.org>
In reply to#96116
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web