This script removes both single-line and block comments from Python source code while preserving string literals. It can be used to sanitize or preprocess Python scripts by stripping away unnecessary documentation or commentary.
- Removes triple-quoted block comments (''' ... ''' and """ ... """)
- Preserves string literals within the code
- Skips commented blocks that are not string literals
- Can process multiline Python source files
- Reads the input source line-by-line
- Detects whether the parser is inside a block comment or a string
- Skips characters within comments while appending code to the output
- Writes the cleaned source code without comments
python comm_rm.py < input.py > output.pyinput.py: File containing original Python codeoutput.py: Cleaned version with comments removed
- Accepts a list of lines from a Python file
- Returns a list of lines with comments removed
Input:
def hello():
"""This is a docstring"""
print("Hello") # prints helloOutput:
def hello():
print("Hello")- Python 3.6 or higher
This project is part of CS 3342b – Organization of Programming Languages at Western University. For academic use only.