Python 删除python程序的注释
1 借助sublime删除注释在自己的用户路径下C:\Users\ALG\AppData\Roaming\Sublime Text\Packages\User建立文件remove_comments.py:import sublime_pluginimport sublimeclass RemoveCommentsCommand(sublime_plugin.TextCommand):def run
·
1 借助sublime删除注释
在自己的用户路径下C:\Users\ALG\AppData\Roaming\Sublime Text\Packages\User建立文件remove_comments.py:
import sublime_plugin
import sublime
class RemoveCommentsCommand(sublime_plugin.TextCommand):
def run(self, edit):
comments = self.view.find_by_selector('comment')
for region in reversed(comments):
self.view.erase(edit, region)
这里的默认路径就是刚才的路径:
使用 ctrl+` 打开sublime命令行:
view.run_command('remove_comments')
2 其他脚本
修改这里:
config.py
import os
jklp = r"D:\Desktop\AI算法库-吸烟检测算法V1.2"
algname = os.path.basename(jklp)
执行这里:
huihui1.py
import os
import re
from config import *
def all_path(dirname):
result = []
for maindir, subdir, file_name_list in os.walk(dirname):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
result.append(apath)
return result
path = jklp
result = []
for name in all_path(path):
name1 = os.path.join(path, name)
if name1.endswith(".py"):
try:
singleTxt = open(name1, "r", encoding="utf-8").readlines()
if len(singleTxt) > 10:
result.extend("\nprint(wqeqweuiqdbkasbd1231241241241241)\n")
result.append(f"\nprint qqqqqqqqqqqqqqqqqqqqqqqqqqqq {os.path.basename(name)}\n")
result.extend(singleTxt)
except:
pass
open("result.py", "w", encoding='utf-8').writelines(result)
使用 sublime删除注释后再使用这里:
huihui2.py
import os
import re
from config import *
Txt = open(r"D:\Desktop\result.py", "r", encoding="utf-8").read()
Txt = Txt.replace("print qqqqqqqqqqqqqqqqqqqqqqqqqqqq", "#")
Txt = Txt.replace("print(wqeqweuiqdbkasbd1231241241241241)", f'"""\nCopyright 2017-2022 ------ Inc \n{algname} \n"""')
open("result1.py", "w", encoding='utf-8').write(Txt)
import os
import re
Txt = open("result1.py", "r", encoding="utf-8").readlines()
Txt = list(filter(lambda x: len(x) > 2, Txt))
# os.remove("result2.py")
open(f"{algname}.py", "w", encoding='utf-8').writelines(Txt)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献13条内容
所有评论(0)