我正在尝试替换目录中所有json文件中的文本字符串。目录有多个嵌套目录,目录结构的深度未知。下面是我的代码,它没有替换字符串。
import os
import pathlib
import glob
def main():
replacement = "New"
oldTex = "Old"
baseFolder = '1.5.13_test_2'
fullPath = "/Users/gfKron/path"
for item in glob.glob(fullPath +"/*.json", recursive=True):
temp = []
with open(item, "r") as f:
for line in f:
line.replace(oldTex, replacement)
temp.append(line)
with open(item, "w") as f:
f.writelines(temp)
f.close()
if __name__== "__main__":
main() 任何帮助都是非常感谢的。
发布于 2019-02-04 07:44:16
replace返回替换字符串
字符串在python中是不可变的。
temp.append(line.replace(oldTex, replacement))https://stackoverflow.com/questions/54507648
复制相似问题