Bottom Page

Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 A Neat way to use pathlib
I have a directory structure which is composed of Countries, States or other subdivision, and Cities.
Here's a way to use pathlib objects in a dictionary to containerize the hierarchy

from pathlib import Path
import os
import inspect

class CountryInfo:
    def __init__(self):

        self.home = Path('.')

        self.bpaths = {
            'data': self.home / '..' / 'data',
            'Country':  {
                'country': self.home / '..' / 'data' / 'Country',
                'USA': {
                    'usa': self.home / '..' / 'data' / 'Country' / 'USA',
                    'Alaska': self.home / '..' / 'data' / 'Country' / 'USA' / 'Alaska',
                    'Alabama': self.home / '..' / 'data' / 'Country' / 'USA' / 'Alabama', 
                    'Arkansas': self.home / '..' / 'data' / 'Country' / 'USA' / 'Arkansas',
                    'American Samoa': self.home / '..' / 'data' / 'Country' / 'USA' / 'AmericanSamoa',
                    'Arizona': self.home / '..' / 'data' / 'Country' / 'USA' / 'Arizona'
                    # ...

    def get_dir_contents(self, path):
        dir_dict = None
        if isinstance(path, Path) and path.exists():
            entries = [entry for entry in path.iterdir()]
            dir_dict = {}
            for entry in entries:
                dd = dir_dict[f"'{}'"] = {}
                dd['type'] = self.get_type(entry)
                # print(f'entry: {}, type {type(entry)}')
        return dir_dict

    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):
            elif isinstance(value, Path)  and not value.is_file():

def testit():
    bp = CountryInfo()

    Arizona = bp.bpaths['Country']['USA']['Arizona']
    Scottsdale = Arizona / 'Scottsdale'

    files = bp.get_dir_contents(Scottsdale)
    if files is not None:
        for file in files:
        print('Scottsdale directory is empty')

if __name__ == '__main__':
results of example:
../data/Country/USA/Arizona/Scottsdale/coslicense.pdf ../data/Country/USA/Arizona/Scottsdale/ct_BusinessLicences.csv
Since the elements of the dictionary are pathlib objects, they inherit all of the methods of pathlib
for example:
print(f'Scottsdale path: {Scottsdale.resolve()})
will return the absolute path of the Scottsdale directory (I replaced root paths with ...):
Scottsdale path: .../Data-2TB/BusinessLists/data/Country/USA/Arizona/Scottsdale
Upodate: Added some error checking, and a new method create_all_directories which will create empty directories if they don't already exist.

Also changed the get_dir_contents method to return all contents of directory. Individual entries can be tested for type by adding after line 62:
        for file in files:
            if file.is_dir():
               print(f'{} is a directory')
            elif file.is_file():
               print(f'{} is a regular file')
            elif file.is_symlink():
               print(f'{} is a symbolic link')
               # and so on, see pathlib (3.7 is most complete if running that python version) )docs for all possibilities
Update Jul25: 07:36 EST
added qualifier:
 and not value.is_file()
to line 51, needed to prevent crash if value is path + file, url, etc.
j.crater likes this post

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)