Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: merge_txt.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script merges all '.txt' files in the current working directory into a single saved file.
- Requirements:
- - Python 3.x
- - os module
- Functions:
- - merge_files(input_folder, output_file):
- Merges all '.txt' files found in the specified input folder into the given output file.
- Usage:
- - Ensure Python 3.x is installed.
- - Place this script in the directory containing the '.txt' files to be merged.
- - Run the script. It will create a file named 'merged.txt' containing the merged content.
- Additional Notes:
- - The merged output file will be saved in the same directory as this script.
- - Subdirectories within the current working directory are not searched; only files directly within it are merged.
- """
- import os
- # Function to gather and merge all files into one
- def merge_files(input_folder, output_file):
- """
- Merges all '.txt' files found in the specified input folder into the given output file.
- Args:
- - input_folder (str): Path to the folder containing the '.txt' files.
- - output_file (str): Path to the output file where merged content will be saved.
- """
- with open(output_file, 'w', encoding='utf-8') as output:
- for filename in os.listdir(input_folder):
- file_path = os.path.join(input_folder, filename)
- if os.path.isfile(file_path) and filename.endswith('.txt'):
- with open(file_path, 'r', encoding='utf-8') as file:
- output.write(file.read())
- output.write('\n')
- if __name__ == "__main__":
- # Get the current working directory
- current_directory = os.getcwd()
- # Define the input and output file paths
- input_folder_path = current_directory
- output_file_path = os.path.join(current_directory, 'merged.txt')
- # Merge the files into a single output file
- merge_files(input_folder_path, output_file_path)
- print("Files Merged Together Successfully.\n")
- print(f"The Text File 'merged.txt' Has Been Saved To:\n{current_directory}\n")
- print("Exiting Program... GoodBye!")
Add Comment
Please, Sign In to add comment