Skip to content

Instantly share code, notes, and snippets.

@felsenhower
Created August 27, 2025 10:55
Show Gist options
  • Select an option

  • Save felsenhower/330d630c975cb1b9b64e790dd63ffc46 to your computer and use it in GitHub Desktop.

Select an option

Save felsenhower/330d630c975cb1b9b64e790dd63ffc46 to your computer and use it in GitHub Desktop.
Quick and dirty Python script to censor command output

This is a very simple python script that removes the PWD and username from a command output.

I wrote it to copy and paste Python errors to ChatGPT without giving it all of my personal data.

Put this into ~/bin/censor and have ~/bin on PATH:

#!/usr/bin/env python3

import sys
import os
import re
    
LOGIN = os.getlogin()
RE_LOGIN = re.compile(re.escape(LOGIN.lower()), re.IGNORECASE)
PWD = os.getcwd()
CENSORED = "[censored]"

for line in sys.stdin:
    line = line.replace(PWD, CENSORED)
    line = RE_LOGIN.sub(CENSORED, line)
    print(line, end="")

Then you can do something like this:

$ cat test.py                                                                                                                                                         130 ↵
#!/usr/bin/env python3

def main() -> None:
    raise Exception("Foo")

if __name__ == '__main__':
    main()

$ ./test.py 2>&1 | censor
Traceback (most recent call last):
  File "[censored]/./test.py", line 9, in <module>
    main()
    ~~~~^^
  File "[censored]/./test.py", line 5, in main
    raise Exception("Foo")
Exception: Foo

Note: Always remember to redirect stderr to stdout first (2>&1)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment