FAQ

On Tue, Sep 8, 2015 at 9:03 PM, 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?

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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 5 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedSep 8, '15 at 11:03a
activeSep 8, '15 at 11:15a
posts5
users4
websitepython.org

People

Translate

site design / logo © 2017 Grokbase