Skip to content

Instantly share code, notes, and snippets.

@Anselmoo
Created May 24, 2024 04:27
Show Gist options
  • Save Anselmoo/6c8ba146f7fb9c9e75037580104b1ae2 to your computer and use it in GitHub Desktop.
Save Anselmoo/6c8ba146f7fb9c9e75037580104b1ae2 to your computer and use it in GitHub Desktop.
✨Merge all types of text files 📚 into 1️⃣
import argparse
from pathlib import Path
def merge_textfiles_files(directory, output_file, file_format):
with open(output_file, 'w') as outfile:
for file in sorted(Path(directory).rglob(f'*.{file_format}')):
with open(file, 'r') as infile:
outfile.write(infile.read())
outfile.write("\n\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Merge all text files in a directory and its subdirectories into one file.')
parser.add_argument('directory', type=str, help='The directory containing the text files to merge.')
parser.add_argument('output_file', type=str, help='The name of the output file.')
parser.add_argument('file_format', type=str, help='The file format to merge.')
args = parser.parse_args()
merge_textfiles_files(args.directory, args.output_file, args.file_format)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment