title: How to print Continuous output of Popen when reset character is used
body: <p>I have a snippet of code that runs a video conversion using <code>ffmpeg</code>.</p>
  <p>I read the output to analyze it later in the application, and also want to print it to console, for user feedback.</p>
  <p>So I have</p>
  <pre class="lang-py prettyprint-override"><code>p = Popen(['ffmpeg', *args], stderr=STDOUT, stdout=PIPE)
  while True:
      line = p.stdout.readline()
      if not line: 
          break
      print(line.decode('utf-8'), end='')
  
  p.wait()
  if p.returncode == 0:
      pass
  
  </code></pre>
  <p>Which works, somewhat.  It prints the initial output of the ffmpeg command, as those are simple print statements.</p>
  <p>But once the conversion starts, all the output is updated on one line, presumably using some reset character to move the cursor position back to the start of the line.</p>
  <p>Is there a way to continue to print that output?</p>
  <p>Edit:</p>
  <h1>Answer</h1>
  <pre class="lang-py prettyprint-override"><code>p = subprocess.Popen(args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, universal_newlines=True)
  
  with p.stdout:
      for line in iter(p.stdout.readline, None):
          if not line:
              break
          if line.startswith('frame'):
              print(f'\033[2K{line.strip()}\r', end='')
          else:
              print(line, end='')
  </code></pre>
  <p>This includes the cursor return, and clears the current line before printing to it again.  And still prints the part I want on the same line, like it normally does.</p>