Python文件去除注释的方法
作者:惟愿莲心不染尘 时间:2023-09-02 06:56:17
本文实例讲述了Python文件去除注释的方法。分享给大家供大家参考。具体实现方法如下:
#!/usr/bin/python
# -*- coding: GBK -*-
#writer:xmnathan
#py文件去注释
import re
import os
import ConfigParser
Python='CleanNote'
def ReadIni(path,section,option):#文件路径,章节,关键词
#读取ini
cf=ConfigParser.ConfigParser()
cf.read(path)
value=cf.get(section,option)#如果用getint()则直接读取该数据类型为整数
return value
def IsPassLine(strLine):
#是否是可以忽略的行
#可忽略行的正则表达式列表
RegularExpressions=["""/'.*#.*/'""","""/".*#.*/"""",
"""/'/'/'.*#.*/'/'/'""","""/"/"/".*#.*/"/"/""""]
for One in RegularExpressions:
zz=re.compile(One)
if re.search(zz,strLine)==None:
continue
else:
return True#有匹配 则忽略
return False
def ReadFile(FileName):
#读取并处理文件
fobj=open(FileName,'r')
AllLines=fobj.readlines()
fobj.close()
NewStr=''
LogStr='/n%20s/n'%(FileName.split('//')[-1])#输出的日志
nline=0
for eachiline in AllLines:
index=eachline.find('#')#获取带注释句‘#'的位置索引
if index==-1 or nline<3 or IsPassLine(eachline):
if eachiline.strip()!='':#排除纯空的行
NewStr=NewStr+eachiline
else:
if index!=0:
NewStr=NewStr+eachiline[:index]+'/n'#截取后面的注释部分
LogStr+="ChangeLine: %s/t%s"%(nline,eachline[index:])
nline+=1
return NewStr,LogStr
def MakeCleanFile(SrcPath,DescPath,FileList):
fLog=open(DescPath+'//'+'CleanNoteLog.txt','w')
for File in FileList:
curStr,LogStr=ReadFile(SrcPath+'//'+File)
fNew=open(DescPath+'//'+File,'w')
fNew=write(curStr)
fNew.close()
fLog.write(LogStr)
fLog.close()
def Main():
#从ini获取源文件夹及目标文件夹路径
IniPath=os.getcwd()+'//'+PtName+'.ini'
SrcPath=ReadIni(IniPath,PyName,'SrcPath')#源文件夹
DescPath=ReadIni(IniPath,PyName,'DescPath')#目的文件夹
#如果目的文件夹不存在,创建之
if not os.path.exists(DescPath):
os.makedirs(DescPath)
FileList=[]
for files in os.walk(SrcPath):
for FileName in files[2]:
if FileName.split('.')[-1]=='py':
FileList.append(FileName)
MakeCleanFile(SrcPath,DescPath,FileList)
if __name__=='__main__':
Main()
print '>>>End<<<'
os.system('pause')
ps:配置文件CleanNote.ini的格式
[CleanNote]
SrcPath=E:/test
DescPath=E:/test/newfiles
批量去除指定源文件夹中的py文件的注释,并生成拷贝与指定目的文件夹
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,文件,注释
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL HandlerSocket插件安装配置教程
2024-01-20 09:37:55
![](https://img.aspxhome.com/file/2023/8/99358_0s.png)
13个最常用的Python深度学习库介绍
2023-08-04 03:08:09
SQL Server忘记sa账号密码重新添加新管理账号
2024-01-18 09:59:44
![](https://img.aspxhome.com/file/2023/8/92728_0s.png)
基于Go和PHP语言实现爬楼梯算法的思路详解
2024-05-22 10:18:20
简单介绍Python中的JSON模块
2023-12-30 00:16:29
![](https://img.aspxhome.com/file/2023/7/99007_0s.png)
win10+anaconda安装yolov5的方法及问题解决方案
2022-02-03 10:31:58
![](https://img.aspxhome.com/file/2023/4/83854_0s.png)
使用python生成云词图实现画红楼梦词云图
2022-07-19 00:21:56
![](https://img.aspxhome.com/file/2023/4/131374_0s.jpg)
MySQL全局共享内存介绍
2024-01-14 09:40:14
在ADF中跟踪SQL执行时间实现代码
2024-01-16 16:09:00
Python3 扫描库文件并获取版本号信息的操作方法
2023-08-11 19:14:17
![](https://img.aspxhome.com/file/2023/3/60943_0s.png)
MySQL备份与恢复之保证数据一致性(5)
2024-01-16 19:16:20
![](https://img.aspxhome.com/file/2023/4/104124_0s.jpg)
Python format字符串格式化函数的使用
2023-12-20 12:45:59
![](https://img.aspxhome.com/file/2023/0/82750_0s.png)
关于Python中的同步异步阻塞与非阻塞
2021-09-03 05:54:01
详解MySQL多版本并发控制机制(MVCC)源码
2024-01-20 04:20:38
![](https://img.aspxhome.com/file/2023/1/87121_0s.jpg)
Python使用poplib模块和smtplib模块收发电子邮件的教程
2023-11-02 14:58:34
Python 抓取动态网页内容方案详解
2022-05-19 02:09:29
![](https://img.aspxhome.com/file/2023/7/115827_0s.png)
通过实例了解JS 连续赋值
2024-05-02 16:15:14
![](https://img.aspxhome.com/file/2023/4/132864_0s.png)
python基础教程之Hello World!
2021-03-05 22:45:06
PhpStorm连接服务器并实现自动上传功能
2024-05-02 17:07:02
![](https://img.aspxhome.com/file/2023/6/129866_0s.png)
chatgpt 1020 错误码成功解决的三种方案(推荐)
2023-02-03 17:33:28
![](https://img.aspxhome.com/file/2023/3/104963_0s.png)