import os
path = r"F:\icon"
# 该文件夹下所有的文件(包括文件夹)
filelist = os.listdir(path)
count = 1
for file in filelist:
print(file)
for file in filelist: # 遍历所有文件
Olddir = os.path.join(path, file) # 原来的文件路径
filename = os.path.splitext(file)[0] # 文件名
filetype = os.path.splitext(file)[1] # 文件扩展名
# with open(oldFile, 'r') as source_file, open(newFile, 'a+') as target_file:
with open(Olddir, 'r') as source_file, open( 'target' + str(count) +'.svg', 'a+') as target_file:
# 遍历源文件的每一行
for line in source_file:
# 将需要替换的字符串用新的字符串替换
# new_line = line.replace('old', 'new')
new_line = line.replace('fill="#409eff"', '')
# 将替换后的行写入目标文件
target_file.write(new_line)
# 将目标文件重命名为源文件
# os.rename("newFile", "oldFile")
os.rename('target' + str(count) +'.svg', filename + filetype)
count += 1
Python 批量修改每个文件中的每一行
最新推荐文章于 2024-04-10 17:50:37 发布