的Python:在嵌套文件夹中的所有文件替换文本

问题描述 投票:0回答:1

我试图取代文本字符串在目录中的所有文件的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()   

任何帮助深表感谢。

python string glob
1个回答
0
投票

replace返回替换字符串

字符串在Python不变

temp.append(line.replace(oldTex, replacement))
© www.soinside.com 2019 - 2024. All rights reserved.