Skip to content

Instantly share code, notes, and snippets.

@mandyedi
Created June 25, 2020 12:55
Show Gist options
  • Save mandyedi/7ff9d9b434ac95f69060b043e0142ff8 to your computer and use it in GitHub Desktop.
Save mandyedi/7ff9d9b434ac95f69060b043e0142ff8 to your computer and use it in GitHub Desktop.
python comment remove
'''
The script is intended to remove comment line in soruce files.
The script checks all the files (recursively) in the same folder where it is run.
To change conditions see toBeRemoved(line) function.
'''
import os
def toBeRemoved(line):
if line[:4] == "/***":
return True
elif line[:5] == "//---":
return True
elif line[:6] == "// ---":
return True
elif line[:10] == "// inlined":
return True
elif line[:14] == "// non-inlined":
return True
elif line[:21] == "// This file contains":
return True
else:
return False
for root, dirs, files in os.walk("./"):
for file in files:
originalPath = os.path.join(root, file)
tempPath = originalPath + ".bak"
skip = False
with open(originalPath, "r") as original, open(tempPath, "w+") as temp:
for line in original:
if toBeRemoved(line) == False:
temp.write(line)
else:
skip = True
original.close()
temp.close()
if skip:
os.remove(originalPath)
os.rename(tempPath, originalPath)
else:
os.remove(tempPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment