Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Parse comments from various source files.

Project description

Comment Parser

PyPI status PyPI version PyPI license PyPI pyversions

Python module used to extract comments from source code files of various types.



  • libmagic


sudo pip3 install comment_parser

OSX and Windows

Additionally, complete the special installation requirements for python-magic.


To use, simply run:

>>> from comment_parser import comment_parser
>>> # Returns a list of comment_parser.parsers.common.Comments
>>> comment_parser.extract_comments('/path/to/source_file')

extract_comments signature

def extract_comments(filename, mime=None):
    """Extracts and returns the comments from the given source file.

        filename: String name of the file to extract comments from.
        mime: Optional MIME type for file (str). Note some MIME types accepted
            don't comply with RFC2045. If not given, an attempt to deduce the
            MIME type will occur.
        Python list of parsers.common.Comment in the order that they appear in
            the source file.
        UnsupportedError: If filename is of an unsupported MIME type.

Comments Interface

class Comment(object):
    """Represents comments found in source files."""
    def text(self):
        """Returns the comment's text.

    def line_number(self):
        """Returns the line number the comment was found on.

    def is_multiline(self):
        """Returns whether this comment was a multiline comment.
            True if comment was a multiline comment, False if not.

    def __str__(self):

    def __eq__(self, other):


Running locally

Start python3 in the base of repository.

from comment_parser import comment_parser
comment_parser.extract_comments('foo.c', mime='text/x-c')

Running tests

python3 test

Deploying to PyPi

python3 sdist
twine upload dist/*

Supported Programming Languages

Language Mime String
C text/x-c
C++/C# text/x-c++
Go text/x-go
HTML text/html
Java text/x-java-source
Javascript application/javascript
Shell text/x-shellscript
XML text/xml

And more to come!

Check for corresponding MIME types.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for comment-parser, version 1.0.9
Filename, size File type Python version Upload date Hashes
Filename, size comment_parser-1.0.9-py3.7.egg (21.2 kB) File type Egg Python version 3.7 Upload date Hashes View
Filename, size comment_parser-1.0.9.tar.gz (5.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page