读入多个文件夹并将多个文本文件内容合并到每个文件夹的一个文件中-Python
我是Python新手。我在同一个目录中有100个文件夹,每个文件夹中有多个文本文件。我想将所有文本文件内容合并到每个文件夹中读入多个文件夹并将多个文本文件内容合并到每个文件夹的一个文件中-Python,python,python-3.x,text,directory,operating-system,Python,Python 3.x,Text,Directory,Operating System,我是Python新手。我在同一个目录中有100个文件夹,每个文件夹中有多个文本文件。我想将所有文本文件内容合并到每个文件夹中 Folder1 text1.txt text2.txt text3.txt . . Folder2 text1.txt text2.txt text3.txt . . 我需要输出为将所有文本文件内容复制到一个text1.txt+text2.txt+text3.txt-->Folder1.txt中 我有下面的代码,只是列出了文本文件 for path,subdirs,
Folder1
text1.txt
text2.txt
text3.txt
.
.
Folder2
text1.txt
text2.txt
text3.txt
.
.
我需要输出为将所有文本文件内容复制到一个text1.txt+text2.txt+text3.txt-->Folder1.txt中
我有下面的代码,只是列出了文本文件
for path,subdirs, files in os.walk('./data')
for filename in files:
if filename.endswith('.txt'):
请帮助我如何继续这项任务。谢谢。首先,您需要获取所有文件夹名称,这些名称可以通过。然后迭代所有子对象,对于每个子对象,需要使用相同的函数迭代其所有子对象,同时使用以下方法连接内容: 如果您需要更多帮助,请尝试自己编写,并用代码更新答案 编辑:os.walk可能不是最好的解决方案,因为您知道自己的文件夹结构,只有两个ListDir可以完成这项工作 导入操作系统 basepath='/path/to/directory'可能只是' 对于os.listdirbasepath中的目录名称: dir\u path=os.path.joinbasepath,dir\u name 如果不是os.path.isdirdir\u路径: 持续 使用openos.path.joindir\u路径,dir\u name+'.txt',w'作为输出文件: 对于os.listdirdir\u路径中的文件名: 如果不是文件名“.endswith”.txt”: 持续 file\u path=os.path.joindir\u路径,文件名 使用openfile_路径作为内嵌: 对于填充中的线: outfile.writeline
这不是最好的代码,但它应该能完成任务,而且它是最短的。首先,您需要获取所有文件夹名称,这些名称都可以使用。然后迭代所有子对象,对于每个子对象,需要使用相同的函数迭代其所有子对象,同时使用以下方法连接内容: 如果您需要更多帮助,请尝试自己编写,并用代码更新答案 编辑:os.walk可能不是最好的解决方案,因为您知道自己的文件夹结构,只有两个ListDir可以完成这项工作 导入操作系统 basepath='/path/to/directory'可能只是' 对于os.listdirbasepath中的目录名称: dir\u path=os.path.joinbasepath,dir\u name 如果不是os.path.isdirdir\u路径: 持续 使用openos.path.joindir\u路径,dir\u name+'.txt',w'作为输出文件: 对于os.listdirdir\u路径中的文件名: 如果不是文件名“.endswith”.txt”: 持续 file\u path=os.path.joindir\u路径,文件名 使用openfile_路径作为内嵌: 对于填充中的线: outfile.writeline
这不是最好的代码,但它应该能完成任务,而且是最短的。分解我们需要解决的问题: 查找目录中的所有文件 将所有文件的内容合并到一个文件中-与目录名同名。 然后将此解决方案应用于基本目录中的每个子目录。测试了下面的代码 假设:子文件夹只有文本文件,没有目录
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))
分解问题,我们需要解决方案: 查找目录中的所有文件 将所有文件的内容合并到一个文件中-与目录名同名。 然后将此解决方案应用于基本目录中的每个子目录。测试了下面的代码 假设:子文件夹只有文本文件,没有目录
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))