Search code examples
pythonsortingprogramming-languagesfile-extension

Programmatically determining programming languages based on file extensions in python


If I have a group of random files (source code) in many different languages, is there a library or API I can pass the file extension of each file into for determining which language the file is for, so I may organize these files according to language type? (I need to know the language I cant just sort by file extension)


Solution

  • I ended up using this JSON translated version of Github's Linguist YAML map, for an NPM package, and imported it and just did a dictionary lookup on a set of maps, for any who are interested this is the part of my solution that's relivant:

    language_results = list(map(
            lambda file_args: file_args[0] if file_extension in list(map(
                lambda i: i, file_args[1].get("extensions", []))) else None, language_map.items()))
    language_results = list(filter(None, language_results))
    return language_results[0] if len(language_results) > 0 else None