Python simple implementation fast regular replacement in engineering ~ ~ ~

Source: Internet
Author: User

Write to yourself to see, afraid of their own forgotten

A project to get a transplant, which will involve the header file reference problem, a change too much trouble, decided to use Python regular match replacement. But I don't know how to access all the projects in a folder.

One, so the first step is to try to access all the files in the folder directory

In Python 2.7, Os.walk is used to complete

#coding =utf-8
Import OS
Import re
Dir= ' dir '
For root, subdirs, files in Os.walk (dir):
Print Root #,subdirs,files
Print files
For file in Files:
filename=root+ ' \ \ ' +file
F=open (filename, ' r+ ')
F.write (' Hello Fantasy ')
F.close ()

Root is the generator index to the iteration path, and the following files for the corresponding path of the file is the path can be directly accessed

Test all the files under the path are written to the Hello Fantasy

, because we only need to modify the application of the header file, so we can filter out some files

Come here today, next time.

Above

Python simple implementation fast regular replacement in engineering ~ ~ ~

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.