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

Thank you for your answer. However, as no one has provided an answer, I have chosen to parse the json using the --json flag in python. That is much more work but as I have done it, there is no point in going back. (I have added a nice progress bar using tqdm in python too.)

The original reason why I want to port the script to Windows is that the script was written when I was using linux, and some time later I switched over to Windows but still continued using the bash script using WSL. Over time I found that the only use for WSL is for my backup script. Thus I decided to port the script to python so that I can delete WSL which consumes more than 2GB of space. Originally I tried to do it in a “pythonic” way like below:

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 = output.decode(sys.stdout.encoding).strip()
        if "unchanged " not in line:
            print(line)

But this code still produced output polluted with ANSI codes. I tried a more simple approach where the output was directly piped to findstr and still failed. And then I made this post hoping to see if anyone have some insights.

Alternatively, I have tried using colorama on python, which solved the ANSI code problem but every status output was still printed, instead of staying at the bottom getting replaced.

Your suggestion of PowerShell is interesting. I have PowerShell too but due to this I have continued to use the Windows command line. However as you are redirecting the output to a file, restic might correctly detect it and disabled ANSI escape code output. So the problem may not be solved by switching to PowerShell alone. I’ll try the behavior on PowerShell later when I have time.

And for the reason why I might not want to keep a valid backup - Actually this is rare, but sometimes you will unexpectedly let some files that should not be backed up slip into the backup. For example, when I just started with Python, once I got __pycache__ files and intermediate build files of pyinstaller slipped into the backup. When I see unnecessary files added, I can revert the backup, adjust my exclude criteria, and try again.

I really hope that there will be a --dry-run feature, or there could be something like --no-ansi if the shell detection fails (without needing to go into parsing json). But I understand that this is an open source project and the developer is overwhelmed. So I will accept the current status. I’ll mark the answer solved for now, but I’ll still appreciate better insights from anyone.