【问题标题】:How to merge content from files with same name but in different folders by Python?如何通过Python合并具有相同名称但在不同文件夹中的文件的内容?
【发布时间】:2018-06-07 05:13:28
【问题描述】:

我正在尝试使用 Python 合并来自不同 TXT 文件的内容,但挑战是我只需要合并来自不同文件夹的相同文件名的内容。这是一个截图供您参考:

到目前为止,我可以打印出所有文件名及其完整路径:

import os

for root, dirs, files in os.walk(".", topdown=False):
    for file in files:
        if file.endswith(".txt"):
            filepath = os.path.join(root, file)
            print (filepath)

但是,我如何才能使用 Python 仅合并具有相同名称的文件...仍在研究中。如果您知道答案,请告诉我,或者为我指出更多研究的方法。非常感谢您,节日快乐!

【问题讨论】:

  • 您可以将glob/dir/*/a.txt 等通配符一起使用
  • 同名合并的顺序是什么?按目录名顺序?
  • 你没有提到你的问题是,找到相应的文件或合并它们?
  • @user1767754 谢谢,我会检查一下..
  • @MenglongLi 我要合并同名文件。

标签: python file merge path directory


【解决方案1】:
import os

# create a dictionary with file names as keys
# and for each file name the paths where they
# were found
file_paths = {}
for root, dirs, files in os.walk('.'):
    for f in files:
        if f.endswith('.txt'):
            if f not in file_paths:
                file_paths[f] = []
            file_paths[f].append(root)

# for each file in the dictionary, concatenate
# the content of the files in each directory
# and write the merged content into a file
# with the same name at the top directory
for f, paths in file_paths.items():
    txt = []
    for p in paths:
        with open(os.path.join(p, f)) as f2:
            txt.append(f2.read())
    with open(f, 'w') as f3:
        f3.write(''.join(txt))

【讨论】:

    【解决方案2】:

    您应该执行以下操作,注意:代码未测试。

    import os
    
    mapped_files = {}
    
    for path, subdirs, files in os.walk("."):
        for file in files:
            if file.endswith(".txt"):
                if file in mapped_files:
                    existing = mapped_files[file]
                    mapped_files[file] = existing.append(path)
                else:
                    mapped_files[file] = [path]
    
    for key in mapped_files:
        files = mapped_files[key]
        first_f = os.path.join(path, files[0])
        with open(first_f, "a+") as current_file: 
            for path in files[1:]: # start at the second index
                f = os.path.join(path, key)
                content = open(f,"r").read()
                current_file.write(content) # add all content to the first file
    

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多