Bottom Page

Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 A Neat way to use pathlib
#1
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):
        os.chdir(os.path.dirname(__file__))

        self.home = Path('.')
        self.home.mkdir(exist_ok=True)

        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'
                    # ...
                }
            }
        }
        self.create_all_directories(self.bpaths)

    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"'{entry.name}'"] = {}
                dd['type'] = self.get_type(entry)
                # print(f'entry: {entry.name}, type {type(entry)}')
            print(quit())
        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):
                self.create_all_directories(value)
            elif isinstance(value, Path)  and not value.is_file():
                value.mkdir(exist_ok=True)

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(f'{file}')
    else:
        print('Scottsdale directory is empty')


if __name__ == '__main__':
    testit()
results of example:
Output:
../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 ...):
Output:
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'{file.name} is a directory')
            elif file.is_file():
               print(f'{file.name} is a regular file')
            elif file.is_symlink():
               print(f'{file.name} 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
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)