Piping restic on Windows - how to deal with ANSI escape sequences

Thank you for the code! The approach that I have used is a little bit different:

import json
import os
import subprocess
import sys
import re

def escape_ansi(line):
    ansi_escape = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]')
    return ansi_escape.sub('', line)

# ...

cmds = [
    'restic', '--repo', restic_path_str, 'backup',
    '--verbose', '--verbose',
    '--files-from', files_from_path_str,
    '--exclude-file', exclude_file_str,
    '--json'
]

process = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

while True:
    output = process.stdout.readline()
    if process.poll() is not None and output == b'':
        break
    if output:
        line = escape_ansi(output.decode(sys.stdout.encoding).strip())
        try:
            j = json.loads(line)
            # ...
            # Analyse the resultant dict and show fancy output
        except json.JSONDecodeError:
            # Print the line as is
            print(line)
retval = process.poll()

# ...

I am not an actual progammer either, just doing python programming as my hobby. The escape_ansi function was actually shamelessly copied from StackOverflow…

And I’m not going to put the data into pandas in anyway. My backup script should have nothing to do with data science :joy:

And for how to show properly formatted code in this forum, you can either indent all the code with four spaces, or paste your code between a pair of three backticks like this:

```
Code here
    Indentation preserved!
```

This is markdown syntax FYI

And yep I have also told Windows Defender to ignore restic.exe. Thank you