Created
June 11, 2021 11:22
-
-
Save YatinAdityaT/f2e9118e3a770475a15be30cbdee13fa to your computer and use it in GitHub Desktop.
Remove all comments from a .py file
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
# 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