title: How to execute command line in a python loop?
body: <p>I am trying to determine the best way to execute something in command line using python.  I have accomplished this with <a href="https://docs.python.org/2/library/subprocess.html" rel="nofollow">subprocess.Popen()</a> on individual files.  However, I am trying to determine the best way to do this many time with numerous different files.  I am not sure if I should create a batch file and then execute that in command, or if I am simply missing something in my code.  Novice coder here so I apologize in advance.  The script below returns a returncode of 1 when I use a loop, but a 0 when not in a loop.  What is the best approach for the task at hand? </p>
  
  <pre><code>def check_output(command, console):
      if console == True:
          process = subprocess.Popen(command)
      else:
          process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
      output, error = process.communicate()
      returncode = process.poll()
      return returncode, output, error
  
  for file in fileList.split(";"):
      ...code to create command string...
      returncode, output, error = check_output(command, False)
      if returncode != 0:
          print("Process failed")
          sys.exit()
  </code></pre>
  
  <p>EDIT: An example command string looks like this:</p>
  
  <p>C:\Path\to\executable.exe -i C:\path\to\input.ext -o C:\path\to\output.ext</p>