用python在utf-8编码下将文件名中带有l_english的yml文件另存为l_simp_chinese并将文件内容中的l_english也替换为l_simp_chinese
你可以使用Python的文件操作和字符串操作来完成这个任务。下面是一个示例代码,可以实现将文件名中带有l_english的yml文件另存为l_simp_chinese,并将文件内容中的l_english也替换为l_simp_chinese:
import os
# 获取当前目录下所有文件
files = os.listdir()
for file in files:
if file.endswith(".yml") and "l_english" in file:
# 构建新的文件名
new_file = file.replace("l_english", "l_simp_chinese")
# 读取原文件内容
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
# 替换文件内容中的l_english为l_simp_chinese
new_content = content.replace("l_english", "l_simp_chinese")
# 写入新文件
with open(new_file, 'w', encoding='utf-8') as f:
f.write(new_content)
请确保在运行代码之前,你已经将当前工作目录切换到存放这些yml文件的目录下。另外,注意备份原始文件以防止意外情况的发生
原文地址: http://www.cveoy.top/t/topic/h36d 著作权归作者所有。请勿转载和采集!