Skip to content

Instantly share code, notes, and snippets.

@YatinAdityaT
Created June 11, 2021 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YatinAdityaT/f2e9118e3a770475a15be30cbdee13fa to your computer and use it in GitHub Desktop.
Save YatinAdityaT/f2e9118e3a770475a15be30cbdee13fa to your computer and use it in GitHub Desktop.
Remove all comments from a .py file
# use python clean.py -f <filename>
# not meant to be very smart (can't distinguish "a = "#test"" for example...)
import os
import argparse
import re
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', type=str)
args = parser.parse_args()
with open(args.file, 'r') as f:
lines = f.readlines()
lines = ''.join(lines)
lines = re.sub(r'\"\"\"[^\"]*\"\"\"', '', lines)
lines = re.sub(r'#.*', '', lines)
with open(args.file, 'w') as f:
f.write(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment