here is a pathlib data structure for an application I am working on.
It spans many directories and sub directories and may or may not be of any use for your application, but I offer it just the same:
BizPaths.py:
# Code files found here:
from pathlib import Path
import os
import inspect
class BizPaths:
def __init__(self):
os.chdir(os.path.dirname(__file__))
self.homepath = Path('.')
self.rootpath = self.homepath / '..'
self.datapath = self.rootpath / 'data'
self.datapath.mkdir(exist_ok=True)
self.dbpath = self.datapath / 'database'
self.dbpath.mkdir(exist_ok=True)
self.jsonpath = self.datapath / 'json'
self.jsonpath.mkdir(exist_ok=True)
self.rawdatapath = self.datapath / 'RawData'
self.rawdatapath.mkdir(exist_ok=True)
self.countrypath = self.rawdatapath / 'Country'
self.countrypath.mkdir(exist_ok=True)
self.usapath = self.countrypath / 'USA'
self.usapath.mkdir(exist_ok=True)
self.Countries = {
'USA': {
'Alaska': self.usapath / 'Alaska',
'Alabama': self.usapath / 'Alabama',
'Arkansas': self.usapath / 'Arkansas',
'AmericanSamoa': self.usapath / 'AmericanSamoa',
'Arizona': self.usapath / 'Arizona',
'California': self.usapath / 'California',
'Colorado': self.usapath / 'Colorado',
'Connecticut': self.usapath / 'Connecticut',
'WashingtonDC': self.usapath / 'WashingtonDC',
'Delaware': self.usapath / 'Delaware',
'Florida': self.usapath / 'Florida',
'FederatedStatesOfMicronesia': self.usapath / 'FederatedStatesOfMicronesia',
'Georgia': self.usapath / 'Georgia',
'Guam': self.usapath / 'Guam',
'Hawaii': self.usapath / 'Hawaii',
'Iowa': self.usapath / 'Iowa',
'Idaho': self.usapath / 'Idaho',
'Illinois': self.usapath / 'Illinois',
'Indiana': self.usapath / 'Indiana',
'Kansas': self.usapath / 'Kansas',
'Kentucky': self.usapath / 'Kentucky',
'Louisiana': self.usapath / 'Louisiana',
'Massachusetts': self.usapath / 'Massachusetts',
'Maryland': self.usapath / 'Maryland',
'Maine': self.usapath / 'Maine',
'MarshallIslands': self.usapath / 'MarshallIslands',
'Michigan': self.usapath / 'Michigan',
'Minnesota': self.usapath / 'Minnesota',
'Missouri': self.usapath / 'Missouri',
'Mississippi': self.usapath / 'Mississippi',
'Montana': self.usapath / 'Montana',
'NorthCarolina': self.usapath / 'NorthCarolina',
'NorthDakota': self.usapath / 'NorthDakota',
'NorthernMarianaIslands': self.usapath / 'NorthernMarianaIslands',
'Nebraska': self.usapath / 'Nebraska',
'NewHampshire': self.usapath / 'NewHampshire',
'NewJersey': self.usapath / 'NewJersey',
'NewMexico': self.usapath / 'NewMexico',
'Nevada': self.usapath / 'Nevada',
'NewYork': self.usapath / 'NewYork',
'Ohio': self.usapath / 'Ohio',
'Oklahoma': self.usapath / 'Oklahoma',
'Oregon': self.usapath / 'Oregon',
'Pennsylvania': self.usapath / 'Pennsylvania',
'PuertoRico': self.usapath / 'PuertoRico',
'Palau': self.usapath / 'Palau',
'RhodeIsland': self.usapath / 'RhodeIsland',
'SouthCarolina': self.usapath / 'SouthCarolina',
'SouthDakota': self.usapath / 'SouthDakota',
'Tennessee': self.usapath / 'Tennessee',
'Texas': self.usapath / 'Texas',
'Utah': self.usapath / 'Utah',
'Virginia': self.usapath / 'Virginia',
'VirginIslands': self.usapath / 'VirginIslands',
'Vermont': self.usapath / 'Vermont',
'Washington': self.usapath / 'Washington',
'Wisconsin': self.usapath / 'Wisconsin',
'WestVirginia': self.usapath / 'WestVirginia',
'Wyoming': self.usapath / 'Wyoming'
}
}
self.create_all_directories(self.Countries)
self.geodatabase = self.dbpath / 'GeoDatabase.db'
self.Idaho_Boise_html = self.Countries['USA']['Idaho'] / 'Boise' \
/ 'https:_www.accessidaho.org_public_sos_corp_search.html'
self.codepath = self.rawdatapath / 'Codes'
self.codepath.mkdir(exist_ok=True)
self.usapath = self.Countries['USA']
self.us_business_infopath = self.rawdatapath / 'US-BusinessInfo'
self.us_business_infopath.mkdir(exist_ok=True)
self.us_business_info_states_html = self.us_business_infopath / 'html'
self.us_business_info_states_html.mkdir(exist_ok=True)
self.us_business_info_url = 'http://us-business.info/directory/'
self.native_american = self.codepath / 'AIAlist.txt'
self.national_names = self.codepath / 'NationalFileDomesticNames.txt'
self.county = self.codepath / 'RawCountyData.txt'
self.county_sub = self.codepath / 'RawCountySub.txt'
self.places = self.codepath / 'national_places.txt'
self.state_cd = self.codepath / 'national_state_codes.txt'
self.school_districts = self.codepath / 'national_schdist.txt'
self.voting_districts = self.codepath / 'national_vtd.txt'
self.congressional_districts = self.codepath / 'national_cd115.csv'
self.native_american_json = self.jsonpath / 'NativeAmericanAreas.json'
self.national_names_json = self.jsonpath / 'NationalFileDomesticNames.json'
self.county_json = self.jsonpath / 'County.json'
self.county_sub_json = self.jsonpath / 'CountySub.json'
self.place_json = self.jsonpath / 'Place.json'
self.state_json = self.jsonpath / 'State.json'
self.school_districts_json = self.jsonpath / 'SchoolDistricts.json'
self.voting_districts_json = self.jsonpath / 'VotingDistricts.json'
self.congressional_districts_json = self.jsonpath / 'CongressionalDistricts.json'
self.create_all_directories(self.Countries)
def get_us_business_city_url(self, state):
"""
state is 2 character abbreviation
"""
filename = self.us_business_info_states_html / f'{state}_state.html'
url = f'{self.us_business_info_url}/{state}'
return filename, url
def get_us_business_state_city_url(self, city, state):
"""
city is full city name all lowercase
state is 2 character abbreviation
"""
return f'{self.us_business_info_url}/{city}-{state}'
def get_dir_contents(self, path):
if isinstance(path, Path) and path.exists():
return [entry for entry in path.iterdir()]
return None
def create_all_directories(self, path):
for key, value in path.items():
if isinstance(value, dict):
self.create_all_directories(value)
elif isinstance(value, Path) and not value.is_file():
value.mkdir(exist_ok=True)
def testit():
bp = BizPaths()
Scottsdale = bp.usapath['Arizona'] / 'Scottsdale'
files = bp.get_dir_contents(Scottsdale)
if files is not None:
for file in files:
print(f'{file}')
else:
print('Scottsdale directory is empty')
if __name__ == '__main__':
testit()