String manipulation in Python: Quotation marks and apostrophes
<p>So I'm having this issue with python: I'm trying to create a string I can use for a gdal operation using os.system on python. My aim is using this operation for a variable number of inputfiles:</p>

<pre><code>os.system('gdal_calc.py -A {0} -B {1} -C {2} --outfile={4} --calc="(1-A)(1-B)(1-C)" '.format(File1, File2, File3, outputfile))
</code></pre>

<p>So here's how my function looked like:</p>

<pre><code>def Mergefilesforoperation(Inputfiles= None):

    ABC = ["A","B","C","D","E","F","G","H","I","J","K",
    "L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    partstring = []

    for i in range(len(Inputfiles)-1):
        partstring.append(" -" + ABC[i] +  " '" + Inputfiles[i] + "'")


    wholestring = "".join(partstring)  
    outputfile = 'outputfile.tif'
    operationstring = "gdal_calc.py" + wholestring + " --outfile= {0}".format(outputfile) + ' --calc="(1-A)*(1-B)*(1-C)*(1-D)"'

    return wholestring, operationstring
</code></pre>

<p>But when I return the fuction with: </p>

<pre><code>File1 = '/home/File1.tif' File2 = '/home/File2.tif' File3 = '/home/File3.tif'

Mergefilesforoperation(Inputfiles=[File1, File2, File3]) 
</code></pre>

<p>it gives me the following output:</p>

<pre><code>Wholestring = " -A '/home/File1.tif' -B '/home/File2.tif' -C '/home/File3.tif'"

operationstring = 'gdal_calc.py -A \'/home/File1.tif\' -B \'/home/File2.tif\' -C \'/home/File3.tif\' --outfile= outputfile.tif --calc="(1-A)(1-B)(1-C)"')
</code></pre>

<p>how do I get rid of the \ sign after the alphabet in "operationstring"?</p>

<p>Do it:</p>

<pre><code>String = ""
for i in range(len(Inputfiles)-1):
    String +=" -" + ABC[i] +  " '" + Inputfiles[i] + "'"
</code></pre>

<p>Instead of:</p>

<pre><code> for i in range(len(Inputfiles)-1):
        partstring.append(" -" + ABC[i] +  " '" + Inputfiles[i] + "'")
</code></pre>

<p>And pay attetion with quotes, we can mix quotes but it is so clear to follow a method.</p>

<p>operationstring = "gdal_calc.py" + wholestring + " --outfile= {0}".format(outputfile) + ' --calc="(1-A)<em>(1-B)</em>(1-C)*(1-D)"'</p>

<p>operationstring = "gdal_calc.py" + wholestring + " --outfile= {0}".format(outputfile) + <strong>"</strong> --calc=<strong>'</strong>(1-A)<em>(1-B)</em>(1-C)*(1-D)<strong>'</strong> <strong>"</strong></p>

<p>With this changes it may work. I tried it.</p>

python string operating-system