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 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.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.

Image for post
Image for post

Written by

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

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