【问题标题】:Read in multiple folder and combine multiple text files contents to one file per folder - Python读取多个文件夹并将多个文本文件内容合并到每个文件夹一个文件中 - Python
【发布时间】:2020-09-05 06:54:53
【问题描述】:

我是 Python 新手。我在每个文件夹内的同一个目录中有 100 个多个文件夹,每个文件夹都有多个文本文件。我想将所有文本文件内容合并到每个文件夹中。

Folder1
text1.txt
text2.txt
text3.txt
.
.

Folder2
text1.txt
text2.txt
text3.txt
.
.

我需要输出将所有文本文件内容复制到一个 text1.txt + text2.txt + text3.txt ---> Folder1.txt

Folder1
text1.txt
text2.txt
text3.txt
Folder1.txt

Folder2
text1.txt
text2.txt
text3.txt
Folder2.txt  

下面的代码只列出了文本文件。

for path,subdirs, files in os.walk('./data')
    for filename in files:
        if filename.endswith('.txt'):

请帮助我如何继续这项任务。谢谢。

【问题讨论】:

    标签: python python-3.x text directory operating-system


    【解决方案1】:

    首先您需要获取所有文件夹名称,这可以使用os.listdir(path_to_dir) 完成。然后你遍历所有这些,对于每个你需要使用相同的函数遍历它的所有子,同时使用这个连接内容:https://stackoverflow.com/a/13613375/13300960

    如果您需要更多帮助,请尝试自己编写并使用您的代码更新答案。

    编辑:os.walk 可能不是最佳解决方案,因为您知道自己的文件夹结构,而只需两个 listdirs 就可以完成这项工作。

    import os
    
    basepath = '/path/to/directory' # maybe just '.'
    for dir_name in os.listdir(basepath):
        dir_path = os.path.join(basepath, dir_name)
        if not os.path.isdir(dir_path):
            continue
        with open(os.path.join(dir_path, dir_name+'.txt') , 'w') as outfile:
            for file_name in os.listdir(dir_path):
                if not file_name.endswith('.txt'):
                    continue
                file_path = os.path.join(dir_path, file_name)
                with open(file_path) as infile:
                    for line in infile:
                        outfile.write(line)
    

    这不是最好的代码,但它应该可以完成工作并且它是最短的。

    【讨论】:

      【解决方案2】:

      分解我们需要解决的问题:

      1. 查找目录中的所有文件
      2. 将所有文件的内容合并到一个文件中 - 与目录名称相同。

      然后将此解决方案应用于基目录中的每个子目录。测试了下面的代码。

      假设:子文件夹只有文本文件,没有目录

      import os
      
      
      # Function to merge all files in a folder
      def merge_files(folder_path):
          # get all files in the folder,
          # assumption: folder has no directories and all text files
          files = os.listdir(folder_path)
      
          # form the file name for the new file to create
          new_file_name = os.path.basename(folder_path) + '.txt'
          new_file_path = os.path.join(folder_path, new_file_name)
      
          # open new file in write mode
          with open(new_file_path, 'w') as nf:
              # open files to merge in read mode
              for file in files:
                  file = os.path.join(folder_path, file)
                  with open(file, 'r') as f:
                      # read all lines of a file and write into new file
                      lines_in_file = f.readlines()
                      nf.writelines(lines_in_file)
                      # insert a newline after reading each file
                      nf.write("\n")
      
      
      # Call function from the main folder with the subfolders
      folders = os.listdir("./test")
      for folder in folders:
          if os.path.isdir(os.path.join('test', folder)):
              merge_files(os.path.join('test', folder))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-06
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        • 2023-01-18
        相关资源
        最近更新 更多