How to validate a RAR file in Python

Archive validation is definitely no cakewalk when trying to write the solution yourself, especially if you are trying to be thorough in covering potential errors, etc. So rather than waste our afternoon, what do you say I show you the shortcut? In Python, this can be made significantly easier through simple use of an API from Cloudmersive. Let’s look at how that can be implemented in under five minutes.

Begin by pip installing the client:

pip install cloudmersive-convert-api-client

Then comes the function call, which relies on an API instance, as laid out here:

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.ValidateDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Validate a RAR Archive file (RAR)api_response = api_instance.validate_document_rar_validation(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ValidateDocumentApi->validate_document_rar_validation: %s\n" % e)

And there you have it! File validation made easy. For more like this, look at the rest of this library.

Image for post
Image for post

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