您的位置:首页 > 编程语言 > Python开发

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

2018-01-25 17:36 806 查看
#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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 脚本
相关文章推荐