How to merge multiple text TXT files together in Python

Today we are going to tackle TXT file merging in Python. This is going to be cake.

We begin by installing our API client through the use of pip install. Here’s the command for that:

pip install cloudmersive-convert-api-client

And now we shall go about writing out our function call. This is going to start with API instantiation using a key, followed by calling merge_document_txt_multi from said instance. Stack your files vertically to preserve your file order in the finished result. Below you will see how that can look:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.MergeDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file1 = '/path/to/file' # file | First input file to perform the operation on.input_file2 = '/path/to/file' # file | Second input file to perform the operation on.input_file3 = '/path/to/file' # file | Third input file to perform the operation on. (optional)input_file4 = '/path/to/file' # file | Fourth input file to perform the operation on. (optional)input_file5 = '/path/to/file' # file | Fifth input file to perform the operation on. (optional)input_file6 = '/path/to/file' # file | Sixth input file to perform the operation on. (optional)input_file7 = '/path/to/file' # file | Seventh input file to perform the operation on. (optional)input_file8 = '/path/to/file' # file | Eighth input file to perform the operation on. (optional)input_file9 = '/path/to/file' # file | Ninth input file to perform the operation on. (optional)input_file10 = '/path/to/file' # file | Tenth input file to perform the operation on. (optional)try:# Merge Multple Text (TXT) Files Togetherapi_response = api_instance.merge_document_txt_multi(input_file1, input_file2, input_file3=input_file3, input_file4=input_file4, input_file5=input_file5, input_file6=input_file6, input_file7=input_file7, input_file8=input_file8, input_file9=input_file9, input_file10=input_file10)pprint(api_response)except ApiException as e:print("Exception when calling MergeDocumentApi->merge_document_txt_multi: %s\n" % e)

And that, my friends, is how you get things done in a hurry.

Image for post
Image for post
This guy wished for instant merging for TXT files. He got his wish!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store