Skip to content

Instantly share code, notes, and snippets.

@thom-vend
Created July 1, 2021 02:38
Show Gist options
  • Save thom-vend/cf07168354bc147246c7b26de6ae3fa4 to your computer and use it in GitHub Desktop.
Save thom-vend/cf07168354bc147246c7b26de6ae3fa4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import sys
import argparse
import logging
# Edit this function to change the behavior
def transformation(content):
content = content.decode('utf-8').replace('\u200c', '')
return content.encode('utf-8')
def editfileinplace(filepath, nobackup):
content = None
with open(filepath, 'rb') as f:
content = f.read()
if not nobackup:
with open(f"{filepath}.bak", 'wb') as f:
f.write(content)
updated_content = transformation(content)
with open(filepath, 'wb') as f:
f.write(updated_content)
def main():
parser = argparse.ArgumentParser(description='Do some text/sed like operation on file, in place')
parser.add_argument('filename', type=str, help='Path to file to work on')
parser.add_argument('-i', action="store_true", dest="nobackup", default=False, help='Edit in place, nobackup .bak')
args = parser.parse_args()
if not os.path.exists(args.filename):
logging.error(f"File not found: {args.filename}")
sys.exit(127)
else:
logging.info(args.filename)
editfileinplace(args.filename, args.nobackup)
if __name__ == "__main__":
logging.basicConfig()
main()
@thom-vend
Copy link
Author

Example of usage

find . -type f -iname '*.json' -print0 |xargs -0 -P 64 -n 1 -- edit-unicode-char.py

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