title: How do I 'properly' use executable programs in python
body: <h1>Problem</h1>
  <p>I have a third-party program (written in C++) that I want to use in my Python code. The program takes three arguments as follows:</p>
  <pre><code>$ program.exe [arg1] [input_file] [output_file]
  </code></pre>
  <p>The way I'm using it for now is by writing the input file, use <code>subprocess.run()</code> and read the output file like this:</p>
  <pre><code>import subprocess
  
  def execute_program(arg1, input):
      with open('file.in', 'w') as f:
          f.write(input)
  
      cmd = ['program.exe', arg1, 'file.in', 'file.out']
      completed_process = subprocess.run(cmd, stderr=subprocess.PIPE)
  
      with open('file.out', 'r') as f:
          result = f.read()
  
      return result
  </code></pre>
  <p>Now there are several problems with this, like taking care that the files are erased after execution, having to work with timestamps (or other solutions) when running the code in parallel and overall it just <em>feels</em> plain ugly.</p>
  <hr />
  <h1>Question</h1>
  <p>Is there a &quot;cleaner&quot; way of doing this? Can I pass the input and capture the output directly
  through Python or is there no way of avoiding writing (temporary) files?</p>