Created
June 25, 2020 12:55
-
-
Save mandyedi/7ff9d9b434ac95f69060b043e0142ff8 to your computer and use it in GitHub Desktop.
python comment remove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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