In my quest to learn more regarding programming in Python, I "dissect" a lot of other people's code.
While doing this, or writing my own code, I end up with 10's of debug print lines.

Things like this: if v_Debug == 1: print("DEBUG - ".format()) # DEBUG

If I decide to keep the code for future use, those debug lines must eventually be removed, and that's not very fun at all.

So I decided to use what I had learned about writing self-modding code and create a module that would read thru
the calling module and delete those lines, and then save the altered file.

The code is the whole importable module. To use it, import your module of it, then call f_RemoveDEBUGLines()
with your pattern. The default pattern is "DEBUG". It will read the calling module's file and delete ALL the lines
that have the matching pattern.

Make sure you have a BACKUP of the original file, as you cannot undo what it does.

Note: I only share these snippets as a way to pay forward what I have gained from so many others.
My hope is that some will find these snippets and know what can be done, and how, like I so often
have and still do from other's efforts and posts.

Feel free to modify to your liking.


# Imports #
from __future__ import print_function
import sys, os, importlib, inspect, locale, time
from datetime import datetime

    # Configurations #
locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'

    # Main #
def main():


    # Functions #
        # Normal Functions

def f_RemoveDEBUGLines(v_Pattern = "*DEBUG*"):
    """Syntax: Takes STR, Returns: Self-modified file of self [NOT A COPY, BUT OF SELF];
    Desc.: Searches the running module's (self) for the pattern and then deletes that entire line.
    The result is that all lines with that pattern are removed from the file and the file rewritten.
    Default pattern is "*DEBUG*"
    Test: f_RemoveDEBUGLines() ## Place test pattern in file, save, run the func"""
    import fnmatch
        # This block is to assure that the calling module's filename is used, not the called module
    v_CallingModPath = sys.argv[0] ## Get calling module's path
    v_CallingModPathSplit = v_CallingModPath.split("\\") ## Splits the calling mod's path on '\'
    v_SourceFile = v_CallingModPathSplit[len(v_CallingModPathSplit) -1] ## Assigns the resulting file name at end of split to v_SourceFile

        # Does the lifting now that the proper filename has benn found
    v_Conn = open(v_SourceFile,"r+") ## Opens source file in ' r/w' mode
    v_Lines = v_Conn.readlines()  ## Reads source file's lines into memory ## Reset the v_Conn-pointer (The method seek() sets the file's current position at the offset.)
    for line in v_Lines: ## Gets each  specific line to be checked
        ## Ignore the function itself; Ignore the fnmatch lines; Find the pattern
        if fnmatch.fnmatch(line, "*f_RemoveDEBUGLines*") == True or\
        fnmatch.fnmatch(line, "*fnmatch*") == True or\
        fnmatch.fnmatch(line, v_Pattern) !=  True: ## See if the search pattern is in the line
            v_Conn.write(line) ## If the pattern search was not found in the line, write the line back to the file
    v_Conn.truncate() ## Truncate to remove everything after the last write.

    # Main Loop #
if __name__ == '__main__':

Very nice! Just what I was looking for.

commented: Glad it was a benefit. +0

Today's compilers are optimal, meaning comments don't make it into the final code, so you can just comment those lines, i.e. the if statement and everything beneath it. It is almost a certainty that you will want them again. The input will change or the program will have to be changed and you will want to know what is happening at those check points, so you can then uncomment them.

commented: The reason I wrote it is that some of the programs were rendered unreadble, even by me later. So it gets rid of the detritus. +0

It's a lot easier to use the logging module and simply change the logging level when you no longer want to see the output.

commented: Thanks, however, I want to remove them, and this does it. On smaller programs, I do it manually. Overall, it was more an excersie in "can I?" +0