Advertisement
Python253

merge_files

Mar 1st, 2024 (edited)
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: merge_files.py
  3. # Author: Jeoi Reqi
  4. #
  5. # This script merges all .txt files in a specified folder into a single saved file.
  6.  
  7. import os
  8.  
  9. # Function to gather and merge all files into one
  10. def merge_files(input_folder, output_file):
  11.     with open(output_file, 'w', encoding='utf-8') as output:
  12.         for filename in os.listdir(input_folder):
  13.             file_path = os.path.join(input_folder, filename)
  14.             if os.path.isfile(file_path) and filename.endswith('.txt'):
  15.                 with open(file_path, 'r', encoding='utf-8') as file:
  16.                     output.write(file.read())
  17.                     output.write('\n')  # Add a newline after each file if needed
  18.  
  19. if __name__ == "__main__":
  20.     input_folder_path = r'~\I-SOON\InitialTranslations\LG-TRANS1\TXT\en\Translate\translated_data'  # Replace with the folder containing the .txt files
  21.     output_file_path = r'~\I-SOON\InitialTranslations\LG-TRANS1\TXT\en\Translate\merged_output.txt'  # Replace with the desired output file name
  22.  
  23.     merge_files(input_folder_path, output_file_path)  # Merge the files into a single custom named file
  24.  
  25.     print("Files merged successfully.")  # Terminal output
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement