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

后端 未结 9 2389
小蘑菇
小蘑菇 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:54

    Use:

    pip3 install manip
    

    This lets you use a decorator to create something like:

    @manip(at='.php$', recursive=True) # to apply to subfolders
    def replace_on_php(text, find, replacement):
        return text.replace(find, replacement)
    

    Now in your prompt you should be able to call

    replace_on_php('explode', 'myCustomExplode', path='./myPhPFiles', modify=True)
    

    and this should make the function apply itself on the entire folder.

提交回复
热议问题