title: subprocess breaking pipes and file busy
body: <p>I am trying to do some file system stuff using python, since I don't want to deal with complex shell scripting and would rather confine all my programming to python insofar as possible.  The shell command in 'search_string' reads  filenames in a directory and writes the first 10 names into a file .</p>
  
  <pre><code>search_string = "find " + rootDir + "/"+str(k)  +" -iname \"*\" -type f | head -10  &gt;&gt; negatives" + str(i) + ".txt"
  print(search_string)
  subprocess.call(search_string, shell=True)
  </code></pre>
  
  <p>This code works on my ubuntu 14.04 pc but not on aws which is where this has to run eventually, giving the error:</p>
  
  <pre><code>find: `standard output': Broken pipe
  find: write error
  
  ubuntu@ip:$ uname -r
  3.13.0-37-generic
  </code></pre>
  
  <p>so I decided to write the long shell command to a file which i figured would be easy to call (after making the shell script file executable with the chmod command):</p>
  
  <pre><code>search_string = "sudo find " + rootDir + "/"+str(k)  +" -iname \"*\" -type f | head -10  &gt;&gt; trainingfiles/negatives" + str(i) + ".txt"
  f=open("cmd.sh","w")
  f.write(search_string)
  f.flush()
  os.fsync(f.fileno())
  f.close
  p1=subprocess.Popen(["chmod","+x","/home/www-data/web2py/applications/fingerPrint/modules/classifier_stuff/cmd.sh"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  stdout, stderr = p1.communicate()
  output = p1.communicate()[0]
  print('stdout:'+stdout+' stderr:'+stderr)
  sys.stdout.flush()
  
  p2=subprocess.Popen(["sudo /home/www-data/web2py/applications/fingerPrint/modules/classifier_stuff/cmd.sh"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  stdout, stderr = p2.communicate()
  print('stdout:'+stdout+' stderr:'+stderr)
  sys.stdout.flush()
  </code></pre>
  
  <p>but I get </p>
  
  <pre><code>stdout: stderr:
  Traceback (most recent call last):
  File "prepare_and_train.py", line 56, in &lt;module&gt;
   p2=subprocess.Popen(["/home/www-data/web2py/applications/fingerPrint/modules/classifier_stuff/cmd.sh"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
      errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
      raise child_exception
  OSError: [Errno 26] Text file busy
  </code></pre>
  
  <p>If I change PIPE to STDOUT I get an interesting</p>
  
  <pre><code>OSError: [Errno 9] Bad file descriptor
  </code></pre>
  
  <p>and the same 'file busy' when I try subprocess.call : </p>
  
  <pre><code>sudo: unable to execute ./cmd.sh: Text file busy
  stdout: stderr:
  </code></pre>
  
  <p>I don't really care how I do it, I just want working code - any tips here? I am (prob. obviously) pretty new to linux</p>