python编程多行输入_如何编写Python脚本替换文件中的多行字符?

此博客介绍了如何使用Python编写一个名为comment.py的脚本,该脚本用于评论指定目录或文件中的特定代码段,支持C、H文件的注释操作。它通过查找版权声明来定位代码范围并进行注释,适用于自动化代码维护任务。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python#Filename: comment.pyimportos, sys, fileinput#-------------------------------------------------------------defusage():printu'''help: comment.py

[dirname]: Option, select a directory to operate

[filename]: Option, select a file to operate

Example: python comment.py /home/saylor/test'''#--------------------------------------------------------------defcommentFile(src, fileList):'''description: comment files

param src: Operate file name'''#if file exist?ifnotos.path.exists(src):print'Error: file - %s doesn\'t exist.'% srcreturnFalseifos.path.islink(src):print'Error: file - %s is just a link, will not handle it.'returnFalse

filetype=(os.path.splitext(src))[1]ifnotfiletypein['.c','.h']:returnFalsetry:ifnotos.access(src, os.W_OK):

os.chmod(src,0664)except:print'Error: you can not chang %s\'s mode.'% srctry:

inputf=open(src,'r')

outputfilename=src+'.tmp'outputf=open(outputfilename,'w')

beginLine=0

endLine=100000000isMatched=False#-----find the beginLine and endLine -------------------foreachlineinfileinput.input(src):ifeachline.find('/*')>=0:

beginLine=fileinput.lineno()ifeachline.find('Copyright 2002 Sun Microsystems, Inc. All rights reserved.')>=0:

isMatched=Trueifeachline.find('*/')>=0andisMatched:

endLine=fileinput.lineno()break#-----delete the content between beginLine and endLine-----printbeginLine, endLine

lineNo=1foreachlineininputf:iflineNo

outputf.write(eachline)eliflineNo>endLine:printeachline

outputf.write(eachline)

lineNo=lineNo+1inputf.close()

outputf.close()

os.rename(outputfilename, src)

fileList.append(src)except:print'Error: unexcept error.'inputf.close()

outputf.close()returnTrue#--------------------------------------------------------------defcommentDir(src, fileList):'''description:

comment files in src(dir)

param src:

operate files in src(dir)'''#if dir exist?ifnotos.path.exists(src):print'Error: dir - %s is not exist.'%s (src)returnFalse

filelists=os.listdir(src)foreachfileinfilelists:

eachfile=src+'/'+eachfileifos.path.isdir(eachfile):

commentDir(eachfile, fileList)elifos.path.isfile(eachfile):

commentFile(eachfile, fileList)returnTrue#--------------------------------------------------------------defmain():iflen(sys.argv)<2:

usage()

sys.exit(1)

src=sys.argv[1]ifos.path.isdir(src):

dire=os.path.abspath(src)

dirFlag=Trueelifos.path.isfile(src):

fl=os.path.abspath(src)

dirFlag=Falseelse:print'Error'fileList=[]ifdirFlag:

commentDir(dire, fileList)else:

commentFile(fl, fileList)iffileList:print'Successful handle file: ...'foreachfileinfileList:printeachfileprint'Done'returnTrue#--------------------------------------------------------------if__name__=='__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值