Skip to content

Instantly share code, notes, and snippets.

@97997
Last active December 17, 2020 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 97997/d5aad0c04acd8d75dfe8e07da5e91230 to your computer and use it in GitHub Desktop.
Save 97997/d5aad0c04acd8d75dfe8e07da5e91230 to your computer and use it in GitHub Desktop.
FolderInterface.py
# Public class members #
# __init__ constructor: can take one optional argument which is the full path of the desired folder
# if no argument is given class will promt user to select a folder
# files -> tuple: tuple of basename files in folder
# folders -> tuple: tuple of foldernames in folder
# path -> string: string of the selected directory
# all_files_on_subfolders_fullpath -> tuple: tuple of strings containing full path of files in both base folder
# and subfolders
# all_files_on_subfolders_onlybasename -> tuple: tuple of strings containing basename of files in both base folder
# and subfolders
class Folder:
def get_files_with_extension_fullpath(self, list_filetype):
requested_files = []
for file in self.all_files_on_subfolders_fullpath:
if file.endswith(list_filetype):
requested_files.append(file)
return requested_files
def get_files_with_extension_onlybasename(self, list_filetype):
requested_files = []
for file in self.all_files_on_subfolders_onlybasename:
if file.endswith(list_filetype):
requested_files.append(file)
return requested_files
def _get_allfiles_on_subfolders(self, working_directory):
import os
for individualFile in os.listdir(working_directory):
totalpath = working_directory + individualFile
if os.path.isfile(totalpath):
self.all_files_on_subfolders_fullpath.append(totalpath)
self.all_files_on_subfolders_onlybasename.append(os.path.basename(totalpath))
elif os.path.isdir(totalpath):
self._get_allfiles_on_subfolders(totalpath + '/')
def _get_contents(self):
import os
for individualFile in os.listdir(self.path):
absolute_path = self.path + '/' + individualFile
if os.path.isfile(absolute_path):
self.files.append(individualFile)
elif os.path.isdir(absolute_path):
self.folders.append(individualFile)
self._get_allfiles_on_subfolders(self.path + '/')
def _choose_dir(self):
input("Press enter to select folder\n")
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askdirectory()
self.path = file_path
self._get_contents()
def __init__(self, path=""):
self.files = []
self.folders = []
self.all_files_on_subfolders_fullpath = []
self.all_files_on_subfolders_onlybasename = []
self.path = path
if self.path == "":
self._choose_dir()
else:
import os
if self.path != "" and os.path.isdir(self.path):
self._get_contents()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment