Python脚本- 递归遍历文件夹,获取指定扩展名文件,修改文件内容

#USAGE:
# 1、Choose file path
# 2、Choose file type (according to extension name)
# 3、Judging condition
# 4、The content to insert
# 5、Print file path
import os
#1、related file path
path = '/Users/userName/Desktop/projectName'
#file number count
x = 0
for dirpath, dirnames, filename in os.walk(path):
    for file in filename:
        # 2、os.path.splitext(): split file name and extension name.
        if os.path.splitext(file)[1] =='.cs':
            fullpath = os.path.join(dirpath,file)
            x += 1
            fo = open(fullpath, 'r')
            str = fo.read().strip()
            #3
            if'2015 Company and Company' in str:
                print(x, fullpath)
            else:
                #4
                newTest = ((
'''
//
//  File Name   %s
//  Project YourProjectName
//
//  Created by Author on 15-10-26.
//  Copyright © 2015  Company. All rights reserved.
//
'''%file) + str).strip()
                with open(fullpath,'w')as f:
                    f.write(newTest)
                    #5
                    print(fullpath)
print(x)

猜你喜欢

转载自blog.csdn.net/MickeyChen_/article/details/79164717
今日推荐