Python Forum
How to assign input file name as logger name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to assign input file name as logger name
#1
Hi,
I have below code,
I want to define csvfile (HATY.csv which is input file name) as my logfile name.

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)04d:%(levelname)s: %(message)s', filename=('HATY'+datetime.today().strftime('%Y%m%d_%H%M%S')+".txt"), filemode='w',datefmt='%Y-%m-%d %H:%M:%S')
import logging import pandas as pd from datetime import datetime class MyData: def __init__(self): self.mode = None def read_data(self, inFile, colNames, mode): try: df_tmp = pd.read_csv(inFile) print(df_tmp.head(2)) logging.debug("Read data successfully") data.mod_data() except OSError as e: logging.debug("File read fail check fail reason below") logging.debug(e.errno) def mod_data(self): print("New Df.......") print(self.df) logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)04d:%(levelname)s: %(message)s', filename=('mylog'+datetime.today().strftime('%Y%m%d_%H%M%S')+".txt"), filemode='w',datefmt='%Y-%m-%d %H:%M:%S') logging.debug("Hello new log") logging.debug("Initialize file read...") data = MyData() data.read_data(csvfile = r'HATY.csv', cols = 'col1', opmode = 'NOM')
Reply
#2
Quote:I want to define csvfile (HATY.csv which is input file name) as my logfile name.
I'm guessing you just want to use HATY as the file prefix, with timestamp unaltered, and don't want the logfile to
be named HAY.csv as stated.
so:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)04d:%(levelname)s: %(message)s', filename=('HATY'+datetime.today().strftime('%Y%m%d_%H%M%S')+'.txt'), filemode='w',datefmt='%Y-%m-%d %H:%M:%S')
Reply
#3
The file name should be automatically align to csv file name in:
data.read_data(csvfile = r'HATY.csv', cols = 'col1', opmode = 'NOM') here HATY if I pass: data.read_data(csvfile = r'XYZ.csv', cols = 'col1', opmode = 'NOM') filename=('XYZ'+datetime.today().strftime('%Y%m%d_%H%M%S')+'.txt')
but I don't want to manually do it, but automatically grep the csv file name from
data.read_data(csvfile = r'XYZ.csv', cols = 'col1', opmode = 'NOM')
Reply
#4
you greatly modified your code after my post, (as seen by clicking 'view edit history' on original post)
so now it looks like my response was entirely out of context!

it looks like inFile is a file pointer, which had to be opened somewhere (nor shown) within the program
wherever that occurs, you need to to capture the 'stem' of the filename, and modify line 24 to read (I am can't show how to do this as you do not show where file was opened):
filename = f"{stem}{datetime.today().strftime('%Y%m%d_%H%M%S')}.txt"

example: (assuming stem was created from inFile file name)

>>> from datetime import datetime >>> stem = 'ziggy' # extract stem from name of inFile >>> filename = f"{stem}{datetime.today().strftime('%Y%m%d_%H%M%S')}.txt" >>> print(filename) ziggy20200803_102219.txt >>>
Reply
#5
I did not open the file anywhere, I directly defined it in line 22. DO I need to modify the code? can you please suggest whre (and how) to change it. Below is my complete code.

import logging import pandas as pd from datetime import datetime class MyData: def __init__(self): self.mode = None def read_data(self, inFile, colNames, mode): try: df_tmp = pd.read_csv(inFile) print(df_tmp.head(2)) logging.debug("Read data successfully") data.mod_data() except OSError as e: logging.debug("File read fail check fail reason below") logging.debug(e.errno) def mod_data(self): print("New Df.......") print(self.df) logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)04d:%(levelname)s: %(message)s', filename=('mylog'+datetime.today().strftime('%Y%m%d_%H%M%S')+".txt"), filemode='w',datefmt='%Y-%m-%d %H:%M:%S') logging.debug("Hello new log") logging.debug("Initialize file read...") data = MyData() data.read_data(csvfile = r'HATY.csv', cols = 'col1', opmode = 'NOM')
Reply
#6
The file has to be opened somewhere
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to share a configured logger between running processes? somhairle69tx 5 3,649 Sep-06-2024, 11:10 PM
Last Post: somhairle69tx
  logger behaviour setdetnet 1 2,076 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  how to write exception error into logger mg24 3 2,594 Nov-15-2022, 04:20 PM
Last Post: insharazzak
  python insert blank line in logger mg24 1 6,289 Nov-02-2022, 08:36 AM
Last Post: snippsat
  python logger help ... save logger into different folder mg24 1 4,383 Oct-25-2022, 03:04 PM
Last Post: snippsat
  Closing logger from other function problem Paqqno 1 2,161 Apr-25-2022, 11:49 AM
Last Post: Gribouillis
  Closing logger to rename directory malcoverc 1 2,273 Apr-19-2022, 07:06 AM
Last Post: Gribouillis
  Reading an Input File DaveG 1 2,307 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  logger option , where is the file? korenron 1 2,956 Apr-25-2021, 01:28 PM
Last Post: snippsat
  Split and sort input file aawaleh 4 4,633 Apr-10-2020, 09:59 PM
Last Post: aawaleh

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.