This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging nested file path
Type: Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Norfeldt, eric.smith
Priority: normal Keywords:

Created on 2020-08-07 08:38 by Norfeldt, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
logger.py Norfeldt, 2020-08-07 08:38 Project with the issue.
Messages (3)
msg374985 - (view) Author: Lasse Nørfeldt (Norfeldt) Date: 2020-08-07 08:38
I have been having a lot of issues when creating loggers that are located in nested folders.

I have attached a .py with a logger module. 

Currently it works if I don't place them in nested folders...
msg374989 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-08-07 10:12
You'll need to explain what behavior you're seeing, and how that differs from what you expect.
msg374993 - (view) Author: Lasse Nørfeldt (Norfeldt) Date: 2020-08-07 11:30
When running a script like this

```
import os
import utils.logger
import utils.stats
import utils.dir
import utils.convert
import re

import CONSTANTS


HTML_DIR_PATH = CONSTANTS.RAW_DATA_DIR + '/html'
folder_contents = [fc for fc in os.listdir(
    HTML_DIR_PATH) if '.html' in fc and 'tablet' in fc and (', ' in fc or ' og ' in fc)]

utils.dir.remove_dir(CONSTANTS.PROCESS_DATA_DIR)
md_output_path = f'{CONSTANTS.PROCESS_DATA_DIR}/md'
utils.dir.create_dir(md_output_path)

convert_logger = utils.logger.LogFile('convert')

for index, filename in enumerate(folder_contents):
    html_text = open(f'{HTML_DIR_PATH}/{filename}', 'r', encoding='utf-8').read()
    md_text = utils.convert.word_html_to_markdown(html_text)

    with open(f'{md_output_path}/{filename}.md', 'w+') as md_file:
        md_file.write(md_text)

    if any([flag in md_text for flag in CONSTANTS.INSPECT_FLAGS]):
        convert_logger.entry('Flag detected!')
    else:
        convert_logger.entry('Success! - no flags detected')

    procent = int(100*(index+1)/len(folder_contents))
    print(f"{index+1}/{len(folder_contents)} ({procent}%)", end='\r')

    if index > 50:
        break

utils.stats.gen_frequency_stats(convert_logger.path)
utils.stats.gen_frequency_stats(utils.logger.EXCIPIENTS.path)
utils.stats.gen_alpha_sort_stats(utils.logger.EXCIPIENTS.path)
os.system(f'cat {utils.logger.EXCIPIENTS.path}.freq.stats')
```

I get an error like this


```
Traceback (most recent call last):
  File "dev/run_convert.py", line 40, in <module>
    utils.stats.gen_frequency_stats(utils.logger.EXCIPIENTS.path)
  File "~/scraper-produktresume.dk/utils/stats.py", line 5, in gen_frequency_stats
    lines = _gen_list_from_lines(path_to_log_file)
  File "~/scraper-produktresume.dk/utils/stats.py", line 24, in _gen_list_from_lines
    with open(path_to_log_file, 'r') as log_file:
FileNotFoundError: [Errno 2] No such file or directory: 'PROCESSED/excipients.log'
```

Was not expecting that since my utils.logger.py looks like this

```
import os
import logging

import CONSTANTS


class LogFile:
    def __init__(self, logger_name):
        if not os.path.exists(CONSTANTS.PROCESS_DATA_DIR):
            os.makedirs(CONSTANTS.PROCESS_DATA_DIR)

        self.path = f'{CONSTANTS.PROCESS_DATA_DIR}/{logger_name}.log'
        self.file_handler = logging.FileHandler(self.path)

        self.logger = logging.getLogger(f'{logger_name}')
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(self.file_handler)

    def entry(self, text):
        self.logger.info(text)


EXCIPIENTS = LogFile('excipients')

```

I'm both creating the file via the lib `os` (just to be sure that was not the case) and in the `self.file_handler`
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85671
2020-08-07 11:30:01Norfeldtsetmessages: + msg374993
2020-08-07 10:12:34eric.smithsetnosy: + eric.smith
messages: + msg374989
2020-08-07 08:38:02Norfeldtcreate