删除*.py文件中的注释
2012-05-16 21:52:24 来源:WEB开发网核心提示: 注意!!不适用于语句中有'#'的情况,否则后果自负方法一s=''for l in open('file1.py').readlines(): if '#'in l: l=l.split('#')[0]+'\n&
注意!!不适用于语句中有'#'的情况,否则后果自负
方法一
s=''
for l in open('file1.py').readlines():
if '#'in l:
l=l.split('#')[0]+'\n'
s+=l
f = open('file1.py','w')
f.write(s)
f.close()
[Python]代码
f = open('file1.py', 'r+')
line = f.readlines()
f.seek(0)
for l in line:
for i in range(len(l)):
if l[i]=='#':
l=l[:i]+' '*(len(l)-i-1)+'\n'
break
f.write(l)
f.close()
更多精彩
赞助商链接
