简介
有时候代码的注释写的太多,反而影响对程序的理解
手动删除所有注释代码,又过于麻烦
所以基于 文件读写,异常处理,正则字符串 等知识
写了这么个小程序
程序特点
在任意位置终端(cmd)中运行此程序,将需要删除注释的.py文件,拖拽到终端窗口中。都可以在源文件路径下,生成无注释版本的文件.py 和 注释的备份 .txt文件
源代码
import re
def file_analysis(old_file_lines, six_quotes, hashtap):
"""标记需要删除的注释的行号,并存入列表"""
i = 0
for line in old_file_lines:
# 符号 # 独占一行
ret_1 = re.match(r"^[^\w]*#+",line)
if ret_1:
hashtap.append(i)
# 符号 """ 独占一行
ret_2 = re.match(r"[ ]*\"\"\"",line)
if ret_2:
# 如果存在类型,函数说明的 """xxxxx""" 之类的,不予删除
ret_2_1 = re.match(r"[^\"]*\"\"\"[^\"]*\"\"\"",line)
if ret_2_1:
pass
else:
six_quotes.append(i)
i += 1
# 将两个"""行号之间所有的行添加到 # 号列表中
while six_quotes != []:
# 从列表中移出最后两个元素
a = six_quotes.pop()
b = six_quotes.pop()
temp = b
while temp <= a:
hashtap.append(temp)
temp += 1
# 返回 # 号列表, 记返回需要删除的所有注释的 行号 集合
return hashtap
def main():
""" 主函数"""
# 1,获取路径,并读取此文件
# 1.1 获取文件名及其路径
print("\r\n"*3)
file_name = input("请输入需要删除注释的目标文件(形如:file.py):")
# 1.2 读取文件
try:
f = open(file_name, "rb")
old_file = f.read()
f.close()
except:
print("无法打开文件:" + file_name)
else:
# 2,处理文件
# 2.1 读取文件成功,文件解码并按行切割成列表
old_file = old_file.decode("utf-8")
old_file_lines = old_file.splitlines()
# 2.2 处理文件并得到需要删除的注释的行号集合
six_quotes, hashtap = list(), list()
hashtap = file_analysis(old_file_lines, six_quotes, hashtap)
# 此时返回值 hashtap列表中,不仅仅包含#,还有"""的行号
try:
# 3,获取 注释和无注释 内容到列表中
# 3.2 去重并排序,得到所有注释行号的列表
comment_list = sorted(set(hashtap))
# 3.3 创建存储(备份)注释文件内容的列表
comment_file = list()
for i in comment_list:
comment = old_file_lines[i]
comment_file.append(comment)
# 创建与源文件总行号相同的列表 0,1,2,3...
new_file_list = list(i for i in range(len(old_file_lines)))
# 删除注释的行号,留下无注释的行号 的列表集合
for i in comment_list:
new_file_list.remove(i)
# 3.4 创建存储(无注释)新文件内容的列表
new_file_lines = list()
for i in new_file_list:
temp = old_file_lines[i]
new_file_lines.append(temp)
except:
print("待处理代码中没有注释")
else:
# 4,在文件路径新建两个文件,并写入数据到文件
ret = re.match(r"([^ ]+).py",file_name)
if ret:
file_name_pre = ret.group(1)
# 5,分别新建 “干净版”文件,和“注释集合”文件
with open(file_name_pre + "(无注释版).py","wb") as f:
for i in new_file_lines:
f.write(i.encode("utf-8"))
f.write("\r\n".encode("utf-8"))
with open(file_name_pre + "(注释存档).txt", "wb") as f:
for i in comment_file:
f.write(i.encode("utf-8"))
f.write("\r\n".encode("utf-8"))
print("\r\n"*3)
print("--------创建成功!!!--------")
print("\r\n"*3)
else:
print("正则字符串,无法识别文件的路径")
if __name__ == "__main__":
main()
Demo示例
print("程序中常见的注释")
注意:此程序将会删除的,会在Demo注释末尾添加 YES,不会删除的 NO
# 这是第一种注释,'#'放在开头(YES)
ret = analysix(data) # 这是第二种注释,'#'跟在某一个语句后面(NO)
"""
第三种注释
有时候需要注释掉某一整块东西的时候,使用这个
(YES)
"""
""" 第四种注释,这是函数或者类的说明(NO)"""
# 这是第四种注释,'#'前面加了空格(YES)
生成的无注释文件
print("程序中常见的注释")
注意:此程序将会删除的,会在Demo注释末尾添加 YES,不会删除的 NO
ret = analysix(data) # 这是第二种注释,'#'跟在某一个语句后面(NO)
""" 第四种注释,这是函数或者类的说明(NO)"""
生成的注释备份文件
# 这是第一种注释,'#'放在开头(YES)
"""
第三种注释
有时候需要注释掉某一整块东西的时候,使用这个
(YES)
"""
# 这是第四种注释,'#'前面加了空格(YES)
————————————————
版权声明:本文为CSDN博主「programminghkl」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/programminghkl/article/details/97433707