Python Forum

Full Version: How to assign input file name as logger name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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')
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')
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')
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
>>>
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')
The file has to be opened somewhere