Created
January 1, 2024 14:25
-
-
Save abeldantas/e63e3c335f765c9b3242e646c158c84e to your computer and use it in GitHub Desktop.
File extension finder in path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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