- 1 year ago
Merging all txt files in multiple folders or bringing all of the files into one single folder
Comment
1 / 1
To merge all text files in multiple folders into one single folder, you can use a combination of file operations in a scripting language like Python. Here's an example script to accomplish this:
pythonimport os
import shutil
# Source folder containing subfolders with text files
source_folder = 'path/to/source_folder'
# Destination folder to store all merged files
destination_folder = 'path/to/destination_folder'
# Iterate through each subfolder in the source folder
for root, dirs, files in os.walk(source_folder):
for file in files:
if file.endswith('.txt'):
source_file = os.path.join(root, file)
destination_file = os.path.join(destination_folder, file)
shutil.copy2(source_file, destination_file)
In this script:
- Set the
source_folder
variable to the path of the folder containing the subfolders with text files. - Set the
destination_folder
variable to the path where you want to store the merged files. - The script uses the
os.walk()
function to iterate through each subfolder in the source folder. - For each file found with a
.txt
extension, it copies the file to the destination folder usingshutil.copy2()
.
After running this script, all the text files from the subfolders will be copied to the destination folder.
Note: Make sure to replace 'path/to/source_folder'
and 'path/to/destination_folder'
with the actual paths on your system.