using mkdir and touch sub-processes sequentially doesn't work
<p>I have an error that I keep encountering repeatedly, sadly without being able to find solution to at the site.</p>

<pre><code>try:
    #create working dir if it doens't exist already
    if not os.path.isdir(WORKINGDIR):
        print '&gt;&gt;&gt;mdkir ',WORKINGDIR
        subprocess.Popen(['mkdir',WORKINGDIR]).wait()
        print os.path.isdir(WORKINGDIR)

    #create output csv file
    outputCSVFile = WORKINGDIR+ '/'+'results.csv'
    if not os.path.isfile(outputCSVFile):
        print '&gt;&gt;&gt; touch',outputCSVFile
        subprocess.check_output(['touch',outputCSVFile])
</code></pre>

<p>Although the line: <code>print os.path.isdir(WORKINGDIR)</code> always prints <code>True</code>, <code>subprocess</code> returns this error:</p>

<blockquote>
  <p>touch: cannot touch
  `/nfs/iil/proj/mpgarch/archive_06/CommandsProfiling/fastScriptsOutput190916/results.csv':
  No such file or directory</p>
</blockquote>

<p>The same error doesn't appear when I use <code>subprocess.checkoutput</code> instead of <code>subprocess.Popen().wait()</code>.
I know that this issue can be solved in many ways (such as using <code>os</code> methods to creates directories and files), but I am interested on why my way isn't working.</p>

<p>Thanks in advance. </p>

<p>EDIT: as some suggested, the problem probably lies with the fact that the program continues too fast after the <code>subprocess.Popen</code>, and hence the issue is solved using <code>subprocess.checkoutput</code> which is probably slower (since it has to wait for output). But still - I don't understand exactly what is happening, since <code>os.path.istdir</code> shows that the dir was created, before continuing to the line that performs <code>touch</code></p>

<p>I suppose you have file permission problems. 
In your path it appears that you are using NFS. Did you already try it on the local file system?</p>

<p>Anyway, you should avoid to use sub processes for simple file operations. </p>

<p>To create a directory:</p>

<pre><code>if not os.path.exists(WORKINGDIR):
    os.makedirs(WORKINGDIR)
</code></pre>

<p>For touch:</p>

<pre><code>import os

def touch(fname, times=None):
    with open(fname, 'a'):
        os.utime(fname, times)

touch(WORKINGDIR+ '/'+'results.csv')
</code></pre>

python python-2.7 unix