Skip to content

Instantly share code, notes, and snippets.

@abeldantas
Created January 1, 2024 14:25
Show Gist options
  • Save abeldantas/e63e3c335f765c9b3242e646c158c84e to your computer and use it in GitHub Desktop.
Save abeldantas/e63e3c335f765c9b3242e646c158c84e to your computer and use it in GitHub Desktop.
File extension finder in path
import os
import sys
def find_unique_extensions(path):
"""
Recursively finds and returns a set of unique file extensions in the given directory.
:param path: Path of the directory to search in.
:return: Set of unique file extensions.
"""
unique_extensions = set()
for root, dirs, files in os.walk(path):
for file in files:
ext = os.path.splitext(file)[1]
if ext: # Filter out files without an extension
unique_extensions.add(ext)
return unique_extensions
def main():
"""
Main function that handles command-line arguments and prints the unique file extensions.
"""
if len(sys.argv) > 2:
print("Usage: python extension_finder.py [path]")
return
path = sys.argv[1] if len(sys.argv) == 2 else os.getcwd()
extensions = find_unique_extensions(path)
if extensions:
print("Unique file extensions found:")
for ext in extensions:
print(ext)
else:
print("No files found.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment