title: Python - Looping through files in directory freezes mid-way
body: <p>I have a simple script set up using ImageMagick to delete all images in a directory that aren't of size 157x200 pixels:</p>
  
  <pre><code>import subprocess, os, sys
  from tqdm import tqdm
  from pathlib import Path
  
  
  def delete_opaque_files():
      pathlist = Path("faces").glob('*.png')
      for path in tqdm(pathlist):
          path_str = str(path)
          command = f"identify -format '%wx%h' {path_str}"
          process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
          output, error = process.communicate()
          if output.decode("utf-8") != "'157x200'":
              print(f"Deleting: {path_str}")
              os.remove(path_str)
  
  
  delete_opaque_files()
  sys.exit(0)
  
  </code></pre>
  
  <p>It should loop through all 14.5k images in the directory. However, tqdm reports the script running through only ~7220 images before the script apparently freezes (tqdm stops updating and nothing more is output to the console). When that happens, I need to manually kill the process in the terminal.</p>
  
  <p>Are there any ways to diagnose why the script is freezing? I'm not seeing any error output.</p>