良玉的博客 点点滴滴,积水成河_良玉的博客_页游、手游linux运维工程师之路

python删除txt文件的前N行

re id="best-content-2841660202" accuse="aContent" class="best-text mb-10" style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: "PingFang SC", "Lantinghei SC", "Microsoft YaHei", arial, 宋体, sans-serif, tahoma; white-space: pre-wrap; word-wrap: break-word; font-size: 16px; line-height: 29px; color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); min-height: 55px;">import os DELNUM=N   i=1           openFileHandle=open('filename','r') writeFileHandle=open('Temp','w') while 1:     line=openFileHandle.readline()     if line:         if i<=DELNUM:             i=i+1             print "找到%d行:%s"%(DELNUM,line)             continue         else:             i=i+1             writeLine='%s\n'%line             writeFileHandle.write(writeLine)     else:         print "匹配%d行失败文件已到末"%DELNUM         break openFileHandle.close() writeFileHandle.close() os.remove('filename') os.rename('Temp','filename') print "All Done!"


标签: python删除指定行数

作者:良玉 分类:Python 浏览:595 评论:0
留言列表
发表评论
来宾的头像