[issue41499] logging nested file path

2020-08-07 Thread Lasse Nørfeldt

Lasse Nørfeldt  added the comment:

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 
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`

--

___
Python tracker 
<https://bugs.python.org/issue41499>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41499] logging nested file path

2020-08-07 Thread Lasse Nørfeldt

New submission from Lasse Nørfeldt :

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...

--
components: Library (Lib)
files: logger.py
messages: 374985
nosy: Norfeldt
priority: normal
severity: normal
status: open
title: logging nested file path
versions: Python 3.8
Added file: https://bugs.python.org/file49374/logger.py

___
Python tracker 
<https://bugs.python.org/issue41499>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com