closing python comand subprocesses
<p>I want to continue with commands after closing subprocess. I have following code but <code>fsutil</code> is not executed. how can I do it?</p>

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

os.system('mkdir c:\\temp\\vhd')
p = Popen( ["diskpart"], stdin=PIPE, stdout=PIPE )
p.stdin.write("create vdisk file=c:\\temp\\vhd\\test.vhd maximum=2000 type=expandable\n")
p.stdin.write("attach vdisk\n")
p.stdin.write("create partition primary size=10\n")
p.stdin.write("format fs=ntfs quick\n")
p.stdin.write("assign letter=r\n")
p.stdin.write("exit\n")
p.stdout.close
os.system('fsutil file createnew r:\dummy.txt 6553600') #this doesnÂ´t get executed
</code></pre>

<p>At the least, I think you need to change your code to look like this:</p>

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

os.system('mkdir c:\\temp\\vhd')
p = Popen(["diskpart"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
p.stdin.write("create vdisk file=c:\\temp\\vhd\\test.vhd maximum=2000 type=expandable\n")
p.stdin.write("attach vdisk\n")
p.stdin.write("create partition primary size=10\n")
p.stdin.write("format fs=ntfs quick\n")
p.stdin.write("assign letter=r\n")
p.stdin.write("exit\n")
results, errors = p.communicate()
os.system('fsutil file createnew r:\dummy.txt 6553600')
</code></pre>

<p>From the <a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow">documentation for <code>Popen.communicate()</code></a>:</p>

<blockquote>
  <p>Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.</p>
</blockquote>

<p>You could replace the <code>p.communicate()</code> with <code>p.wait()</code>, but there is this warning in the <a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait" rel="nofollow">documentation for <code>Popen.wait()</code></a></p>

<blockquote>
  <p><strong>Warning</strong> This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that. </p>
</blockquote>

python subprocess stdout