python windows interpret the values from for loop to cmd command
<p>Here is a code </p>

<pre><code>    from subprocess import Popen, PIPE

saveerr = sys.stderr 
fsock = open('error.log', 'w') 
sys.stderr = sys.stdout = fsock

D = {}

D['\\\\aucb-net-01\\d$'] = '\\\\nasaudc01\\remote_site_sync\\aucb-net-01'
D['\\\\aupw-file-01\\e$'] = '\\\\nasaudc01\\remote_site_sync\\aupw-file-01'

for k,v in sorted(D.items()):
    print (k,":",v) 
    cmd = 'robocopy {} {} /E /MIR /W:2 /R:1'.format(k,v)
    p = Popen(cmd, stdout=PIPE, bufsize=1, universal_newlines=True)
    for line in p.stdout:
        print(line)
</code></pre>

<p>i would like to, insert the value of "K" and "v" in cmd  after robocopy command  , so that with in the for loop it will  perform robocopy for all the  source and destination mentioned in dictionary  D = {} </p>

<p>i would also like that the script checks for failures in the robocopy  output logs in error.log file  </p>

<pre><code>         Total    Copied   Skipped  Mismatch    FAILED    Extras
 Dirs :      2575         0      2575         0         0         0
Files :      6039         0      6039         0         2         0
Bytes :   1.547 g         0   1.547 g         0         0         0
Times :   0:00:53   0:00:00                       0:00:00   0:00:53

Ended : Tue Aug 30 04:32:48 2016
</code></pre>

<p>if two files have failed then the script should send a mail to some email address. </p>

<p>The way in Python to do it would be:</p>

<pre><code>cmd = 'robocopy {} {} /E /MIR /W:2 /R:1'.format(k, v)
</code></pre>

<p>or, in Python 3.6:</p>

<pre><code>cmd = f'robocopy {k} {v} /E /MIR /W:2 /R:1'
</code></pre>

<p><strong>However, this is wrong!</strong> It will fail if <code>k</code> and <code>v</code> have spaces, and it can be a <a href="https://docs.python.org/3/library/subprocess.html#security-considerations" rel="nofollow">security hazard</a> (imagine if <code>k = '; rm -rf /;</code>). The right way to spawn subprocesses is with this:</p>

<pre><code>cmd = ['robocopy', k, v, '/E', '/MIR', '/W:2', '/R:1']
</code></pre>

python windows robocopy