The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Read File From A Single Directory Into A Python Object

TODO: Pull subtitle into page object
Code
import glob
import os

def load_files(dir):
  files = {}

  paths = [
    file for file in glob.glob(f"{dir}/*")
    if os.path.isfile(file)
  ]

  for path in paths: 
    basename = os.path.basename(path).split('.')[0]
    with open(path) as _in:
      files[basename] = _in.read()

  return files


######################

from pprint import pprint

if __name__ == "__main__":
  source_dir = 'glob_test'
  data = load_files(source_dir)
  pprint(data)
Results
{'BRAVO': 'BRAVO file',
 'alfa': 'alfa file',
 'charlie': 'charlie file',
 'delta': 'delta file',
 'echo': 'echo file',
 'fOxtrOt': 'fOxtrOt file',
 'golf': 'golf file',
 'hotel': 'hotel file'}

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Read File From A Single Directory Into A Python Object 

This pulls all the files in a given directory 
into an object with the filename (minus extension)
as the key to the content 


-- code
-- python

import glob
import os

def load_files(dir):
  files = {}

  paths = [
    file for file in glob.glob(f"{dir}/*")
    if os.path.isfile(file)
  ]

  for path in paths: 
    basename = os.path.basename(path).split('.')[0]
    with open(path) as _in:
      files[basename] = _in.read()

  return files


######################

from pprint import pprint

if __name__ == "__main__":
  source_dir = 'glob_test'
  data = load_files(source_dir)
  pprint(data)

-- results/

{'BRAVO': 'BRAVO file',
 'alfa': 'alfa file',
 'charlie': 'charlie file',
 'delta': 'delta file',
 'echo': 'echo file',
 'fOxtrOt': 'fOxtrOt file',
 'golf': 'golf file',
 'hotel': 'hotel file'}

-- /results


-- categories
-- Python 

-- metadata
-- date: 2024-01-09 18:22:16
-- id: 2ajqrj6p
-- site: aws
-- type: post
-- status: published