How to detect the Line Ending Type of a Text File in Python

With just a few lines of code, we will have the ability to determine the corresponding operating system for text file line endings. Buckle up, this is going to be fast.

We can start things up with the installation of our client:

pip install cloudmersive-convert-api-client

Chugging right along, we call our this function next:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom 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.EditTextApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Detect line endings of a text fileapi_response = api_instance.edit_text_detect_line_endings(input_file)pprint(api_response)except ApiException as e:print("Exception when calling EditTextApi->edit_text_detect_line_endings: %s\n" % e)

Finally, feed the TXT files we need to evaluate into edit_text_detect_line_endings, which will in turn provide our line ending types for each file. Take a moment and enjoy the thought of how much sweet time you just saved yourself. Take another moment and consider how much more time the rest of our API library could save.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.