The Python file module replaces the input content script

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.