import re def delComment(filePath): sourcefile=open(filePath) purefile=open(r"C:\Users\noook\Desktop\purefile.py",'w')#位置改成自己想要的,比如"C:\Users\noook\Desktop\1.py" sourcelines=sourcefile.readlines() for line in sourcelines: if not re.match(r' *#', line): #pattern中第一个是空格,第二个是星号,第三个是注释符 purefile.write(line) purefile.close()
目前先删掉单独成行的注释,那些与运行代码同行的注释等一下再补上