简体   繁体   English

读取多个文件夹并将多个文本文件内容合并到每个文件夹一个文件 - Python

[英]Read in multiple folder and combine multiple text files contents to one file per folder - Python

I'm new to Python.我是 Python 的新手。 I have 100's of multiple folders in the same Directory inside each folder i have multiple text files each.我在每个文件夹内的同一目录中有 100 个多个文件夹,每个文件夹都有多个文本文件。 i want to combine all text files contents to one per folder.我想将所有文本文件内容合并到每个文件夹中。

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

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

i need output as copy all text files content in to one text1.txt + text2.txt + text3.txt ---> Folder1.txt我需要 output 将所有文本文件内容复制到一个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  

i have below code which just list out the text files.我有下面的代码,它只是列出了文本文件。

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

please help me how to proceed on the task.请帮助我如何继续执行任务。 Thank you.谢谢你。

Breaking down the problem we need the solution to:分解问题,我们需要解决方案:

  1. Find all files in a directory查找目录中的所有文件
  2. Merge contents of all the files into one file - with the same name as the name of the directory.将所有文件的内容合并到一个文件中 - 与目录名称相同。

And then apply this solution to every sub directory in the base directory.然后将此解决方案应用于基目录中的每个子目录。 Tested the code below.测试了下面的代码。

Assumption : the subfolders have only text files and no directories假设:子文件夹只有文本文件,没有目录

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))

First you will need to get all folder names, which can be done with os.listdir(path_to_dir) .首先,您需要获取所有文件夹名称,这可以通过os.listdir(path_to_dir)来完成。 Then you iterate over all of them, and for each you will need to iterate over all of its children using the same function, while concatenating contents using this: https://stackoverflow.com/a/13613375/13300960然后你遍历所有这些,对于每个你需要使用相同的 function 遍历它的所有孩子,同时使用这个连接内容: https://stackoverflow.com/a/13613375/13300960

Try writing it by yourself and update the answer with your code if you will need more help.如果您需要更多帮助,请尝试自己编写并使用您的代码更新答案。

Edit: os.walk might not be the best solution since you know your folder structure and just two listdir s will do the job.编辑: os.walk可能不是最好的解决方案,因为您知道您的文件夹结构并且只有两个listdir s 可以完成这项工作。

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)

This is not the best code, but it should get the job done and it is the shortest.这不是最好的代码,但它应该可以完成工作并且它是最短的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法使用 Python 读取和写入文件夹中的多个文本文件? - Is there a way to read and write on multiple text files in a folder using Python? 如何从包含 python 中的多个 csv 文件的文件夹中一次读取一个文件 - How to read one file at a time from folder that contains multiple csv files in python python根据文件名中的文本字符将多个文件从一个文件夹移动到另一个文件夹 - python moving multiple files from one folder to the other based on text characters in file name 将多个文本文件合并为一个文本文件? - combine multiple text files into one text file? 删除一个文件夹中的多个文本文件 - Delete multiple text files in one folder 使用 python 将多个未排序的文本文件合并为一个排序文件 - Combine multiple unsorted text files into one sorted file using python Python,easyGUI和sqlite3:使用python读取一个文件夹或多个文件夹中的文件,然后将文件夹名称和文件名添加到数据库 - Python, easyGUI and sqlite3: Using python to read files in one folder or multiple folder, then adding the folder names and filenames to database 如何使用 Python 将文件夹中的 all.txt 文件和 append 其内容读取到 one.txt 文件中? - How to read all .txt files in a folder and append its contents into one .txt file, using Python? Python | 从一个文件夹合并多个csv(100+)文件,同时考虑csv标头 - Python | Combine multiple csv (100+) files from one folder taking csv header into consideration 将sqlite数据库文件(每个文件多个表)合并为一个文件 - Combine sqlite database files (multiple tables per file) into one file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM