1[Email Protected]:/home/liujianzuo/python/test#ls2 passwd rc.local test13[Email Protected]:/home/liujianzuo/python/test#py test1-r EXIT exit/home/liujianzuo/python/test/rc.local4 a total of 0 lines have been modified. 5[Email Protected]:/home/liujianzuo/python/test#py Test1-r Exit exit/home/liujianzuo/python/test/rc.local6 7The New line is :8Line 6th:#Make sure that the script would "EXIT 0" on success or any other9The word was replaced is: exit==>EXITTen One AThe New line is : -16thLine: EXIT 0 -The word was replaced is: exit==>EXIT the - a total of 2 lines have been modified. -[Email Protected]:/home/liujianzuo/python/test#Cat Test1 - #Coding:utf-8 + #!/usr/bin/env python - + Importsys,os,subprocess A #a= "Liujianzuo" at ifLen (SYS.ARGV) < 5: -Exit"argc less than 4;like Python test.py-r prepare the replacement character for the character File_path_name") - - if '- R' inchSYS.ARGV: -Ar1=sys.argv.index ('- R') -ar2=sys.argv[ar1+ 1] inAR3=SYS.ARGV[AR1 + 2] -Filname= SYS.ARGV[AR1 + 3] to Else: +Exit"The first argv must be-r") -Basedir ="%s/%s"%(Os.path.dirname (Filname), Os.path.basename (filname)) theF=file ('%s'%basedir,"r+") *sum1=0 $Numli=0Panax Notoginseng whileTrue: -Line=F.readline () theNumli+=1 + #Print Line A ifAr2inchLine : the #print "Now pos+++>", F.tell () +F.seek (F.tell ()-Len (line)) - #print F.tell () $New_line=line.replace (AR2,AR3) $ f.write (new_line) - Print """ - \033[31;1mthe New line is: \033[0m the Line%s:%s - The word was replaced is: \033[32;1m%s==>%s\033[0mWuyi """%(Numli,new_line.strip (), AR2,AR3) theSum1+=1 - Continue Wu ifline = ="": - Print "file is done bye" About Break - elifline = ="": - Break - Print "\033[33;1m modified the%s line altogether. \033[0m"%sum1 AF.close ()
The Python file module replaces the input content script