Created
July 1, 2021 02:38
-
-
Save thom-vend/cf07168354bc147246c7b26de6ae3fa4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of usage