Created
May 24, 2024 04:27
-
-
Save Anselmoo/6c8ba146f7fb9c9e75037580104b1ae2 to your computer and use it in GitHub Desktop.
✨Merge all types of text files 📚 into 1️⃣
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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