"Go" using Python to modify the contents of a file

Source: Internet
Author: User

Original address: http://blog.csdn.net/xsckernel/article/details/9007517

As a result of the transfer of an arm project under ads to the GNU project, a lot of repetitive changes are needed, such as

[Plain]View Plaincopyprint?
    1. ABC EQU 1

Modified to:

[Plain]View Plaincopyprint?
    1. #define ABC 1

If a manual modification is a waste of time, so the Python script to do the work, found that it is easy to do (previously encountered similar problems are always written in C code, the amount of code and error prone!!) )

The source code is as follows:

[Python]View Plaincopyprint?
  1. def func ():
  2. Ffrom=open ("2440init.s","R")
  3. Fto=open ("2440init1.s","W")
  4. while True:
  5. L = Ffrom.readline ()
  6. if not L:
  7. Break
  8. if ' EQU ' in L:
  9. temp = L.split ("EQU")
  10. Temp1 = ' #define ' + temp[0] + temp[1]
  11. #print Temp1
  12. Fto.write (TEMP1)
  13. Else:
  14. Temp1 = L
  15. Fto.write (TEMP1)
  16. if __name__ = = "__main__":
  17. Func ()


Use a file 2440init.s to test:

[Plain]View Plaincopyprint?
    1. ABC EQU 1
    2. PDS EQU 9


The resulting file 2440init1.s content is as follows:

[Plain]View Plaincopyprint?
    1. #define ABC 1
    2. #define PDS 9



The first thing that's said is to replace the contents of the file Ffrom with FTO Open should be the same file, but found that the write file output stream opened, will automatically empty the file (except append mode) looks like Java performance.

Can be done with the following code

[Python]View Plaincopyprint?
  1. def func ():
  2. input = open ("2440init.s")
  3. lines = Input.readlines ()
  4. Input.close ()
  5. Output = open ("2440init.s",' W ');
  6. For line in lines:
  7. #print Line
  8. if not line:
  9. Break
  10. if ' EQU ' in line:
  11. temp = Line.split ("EQU")
  12. Temp1 = ' #define ' + temp[0] + temp[1]
  13. Output.write (TEMP1)
  14. Else:
  15. Output.write (line)
  16. Output.close ()
  17. if __name__ = = "__main__":
  18. Func ()



If you have a larger project file, you need to traverse every file in the project. If the file contains the specified string, such as #include "appdef.h", replace it with #include "datatype.h":

[Python]View Plaincopyprint?
    1. Import OS
    2. Def direc ():
    3. For D,FD,FL in os.walk ('/home/shichao/gun-ucos '):
    4. For F in FL:
    5. Sufix = Os.path.splitext (f) [1][1:]
    6. if ((Sufix = = ' h ') or (Sufix = = ' C ')):
    7. #print Sufix
    8. Func (d + '/' + f)
    9. def func (filename):
    10. input = open (filename)
    11. lines = Input.readlines ()
    12. Input.close ()
    13. Output = open (filename,' W ')
    14. For line in lines:
    15. if not line:
    16. Break
    17. if ((' appdef.h ' line ) and (' include ') :
    18. temp = Line.split ("Appdef")
    19. Temp1 = temp[0] + ' datatype ' + temp[1]
    20. Output.write (TEMP1)
    21. Else:
    22. Output.write (line)
    23. Output.close ()
    24. if __name__ = = "__main__":
    25. Direc ()

"Go" using Python to modify the contents of a file

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.