Skip to content

Instantly share code, notes, and snippets.

@entirelymagic
Created October 16, 2021 08:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entirelymagic/187a9a3520c6024c9156bbf1619e389e to your computer and use it in GitHub Desktop.
Save entirelymagic/187a9a3520c6024c9156bbf1619e389e to your computer and use it in GitHub Desktop.
A class that takes a folder path and return a list or dictionary with all specific files required
import os
class FileParser:
"""A class to parse for files or directories in a given directory path"""
def __init__(self, folder_path) -> None:
self.folder_path: str = folder_path
self.all_file_list: list = []
self.all_dirs_list: list = []
self.all_specific_files_by_type: dict = {}
self.all_extensions_list: list = [
".json",
".xml",
".jpg",
".png",
".py",
".txt",
".md",
".html",
".css",
".csv",
".xlsx",
".xls",
".pdf",
".doc",
".docx",
]
def get_all_file_names(self) -> list:
for root, dirs, files in os.walk(self.folder_path):
for file in files:
# append the file name to the list
self.all_file_list.append(os.path.join(root, file))
return self.all_file_list
def get_all_dirs_from_path(self) -> list:
for root, dirs, files in os.walk(self.folder_path):
for directory in dirs:
self.all_dirs_list.append(os.path.join(root, directory))
return self.all_dirs_list
def get_specific_file_name(self, file_name: str) -> str or None:
for root, dirs, files in os.walk(self.folder_path):
for file in files:
if file == file_name:
return os.path.join(root, file)
return None
def get_specific_dir_from_path(self, dir_name: str) -> str or None:
for root, dirs, files in os.walk(self.folder_path):
for directory in dirs:
if directory == dir_name:
return os.path.join(root, directory)
return None
def get_all_files_by_a_specific_file_type(self, file_type: str) -> list:
all_files_type: list = []
for root, dirs, files in os.walk(self.folder_path):
for file in files:
if file.endswith(file_type):
all_files_type.append(os.path.join(root, file))
return all_files_type
def get_all_specific_files_by_all_filetypes(self) -> dict:
for root, dirs, files in os.walk(self.folder_path):
for file in files:
for extension in self.all_extensions_list:
if file.endswith(extension):
self.all_specific_files_by_type.setdefault(extension.split('.')[1], []).append(
os.path.join(root, file)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment