且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何加入合并拆分行并将文件中的数据替换为同一字符串

更新时间:2023-02-20 07:44:30

适用于任何级别的文件名嵌套的递归方法:

A recursive approach that works with any level of file name nesting:

from os import linesep

def get_text_from_file(file_path):
    with open(file_path) as f:
        text = f.read()
        return SAK_replace(text)

def SAK_replace(s):
    lines = s.splitlines()
    for index, l in enumerate(lines):
        if l.endswith('.txt'):
            lines[index] = get_text_from_file(l)
    return linesep.join(lines)