我试图取代文本字符串在目录中的所有文件的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()
任何帮助深表感谢。
replace
返回替换字符串
字符串在Python不变
temp.append(line.replace(oldTex, replacement))