View difference between Paste ID: feTHVMnb and NEDCX8NR
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
# Filename: merge_txt.py
4
# Version: 1.0.0
5
# Author: Jeoi Reqi
6
7
"""
8
Description:
9
    - This script merges all '.txt' files in the current working directory into a single saved file.
10
11
Requirements:
12
    - Python 3.x
13
    - os module
14
15
Functions:
16
    - merge_files(input_folder, output_file): 
17
        Merges all '.txt' files found in the specified input folder into the given output file.
18
19
Usage:
20
    - Ensure Python 3.x is installed.
21
    - Place this script in the directory containing the '.txt' files to be merged.
22
    - Run the script. It will create a file named 'merged.txt' containing the merged content.
23
24
Additional Notes:
25
    - The merged output file will be saved in the same directory as this script.
26
    - Subdirectories within the current working directory are not searched; only files directly within it are merged.
27
"""
28
29
import os
30
31
# Function to gather and merge all files into one
32
def merge_files(input_folder, output_file):
33
    """
34
    Merges all '.txt' files found in the specified input folder into the given output file.
35
36
    Args:
37
    - input_folder (str): Path to the folder containing the '.txt' files.
38
    - output_file (str): Path to the output file where merged content will be saved.
39
    """
40
    with open(output_file, 'w', encoding='utf-8') as output:
41
        for filename in os.listdir(input_folder):
42
            file_path = os.path.join(input_folder, filename)
43
            if os.path.isfile(file_path) and filename.endswith('.txt'):
44
                with open(file_path, 'r', encoding='utf-8') as file:
45
                    output.write(file.read())
46
                    output.write('\n')
47
48
if __name__ == "__main__":
49
    # Get the current working directory
50
    current_directory = os.getcwd()
51
52
    # Define the input and output file paths
53
    input_folder_path = current_directory
54
    output_file_path = os.path.join(current_directory, 'merged.txt')
55
56
    # Merge the files into a single output file
57
    merge_files(input_folder_path, output_file_path)
58
59
    print("Files Merged Together Successfully.\n")
60
    print(f"The Text File 'merged.txt' Has Been Saved To:\n{current_directory}\n")
61
    print("Exiting Program...   GoodBye!")
62