Mar-16-2025, 10:18 AM
I am not too sure what you want to do. I don't know what a .env file is. Your logging variable looks like a .json file.
import json from pathlib import Path # don't save in /usr/log, I will use my /home/pedro/temp/ folder path2json = Path('/home/pedro/temp/log_dict.json') path2log = Path('/home/pedro/temp/log/') logging = { 'module1': { 'path': '/usr/log/module1.log', 'level': 'DEBUG' }, 'module2': { 'path': '/usr/log/module2.log', 'level': 'DEBUG' }, 'module3': { 'path': '/usr/log/module3.log', 'level': 'DEBUG' } } # save the logging as json with open(path2json, 'w', encoding='utf-8') as fp: json.dump(logging, fp, ensure_ascii=False, indent=4) # open the data to inspect it with open(path2json) as fp: data_dict = json.load(fp) type(data) # returns <class 'dict'> so we have a Python dictionary # I won't make files in /usr that could cause problems # create the log files in /home/pedro/temp/log/ for key in data_dict.keys(): print(key) print(f'path = {data_dict[key]["path"]}') logfile = Path(data_dict[key]["path"]) savename = logfile.name savepath = path2log / savename with open(savepath, 'a') as sp: sp.write(f'This is {logfile}') # make a list of the files now in path2log file_list = [filename for filename in path2log.iterdir() if filename.is_file()] for filename in file_list: print(filename)Now I have 3 log files in /home/pedro/temp/log/:
Output:/home/pedro/temp/log/module1.log
/home/pedro/temp/log/module3.log
/home/pedro/temp/log/module2.log
Maybe that is something like what you want?