Skip to content

soh2970/Python-comment-remover

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Python Comment Remover

Overview

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.

Features

  • 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

How It Works

  1. Reads the input source line-by-line
  2. Detects whether the parser is inside a block comment or a string
  3. Skips characters within comments while appending code to the output
  4. Writes the cleaned source code without comments

Usage

python comm_rm.py < input.py > output.py
  • input.py: File containing original Python code
  • output.py: Cleaned version with comments removed

Functions

remove_comments(source: list[str]) -> list[str]

  • Accepts a list of lines from a Python file
  • Returns a list of lines with comments removed

Example

Input:

def hello():
    """This is a docstring"""
    print("Hello")  # prints hello

Output:

def hello():
    print("Hello")

Requirements

  • Python 3.6 or higher

License

This project is part of CS 3342b – Organization of Programming Languages at Western University. For academic use only.

About

Python script to remove single-line and block comments from Python source code, preserving string literals.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages