Python - Way to recursively find and replace string in text files

后端 未结 9 2425
小蘑菇
小蘑菇 2020-12-24 01:56

I want to recursively search through a directory with subdirectories of text files and replace every occurrence of {$replace} within the files with the contents of a multi l

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 02:42

    Multiple files string change

    import glob

    for allfiles in glob.glob('*.txt'):

    for line in open(allfiles,'r'):
        change=line.replace("old_string","new_string")
        output=open(allfiles,'w')
        output.write(change)    
    

提交回复
热议问题