Python Forum
unable to write to log file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: unable to write to log file (/thread-28973.html)



unable to write to log file - Mekala - Aug-12-2020

Hi,
I try to run read_data function from main, but although it read data, but not writing to log file.

main.py

import logging
from datetime import datetime
import pandas as pd
from read_data import data
import os
import sys
import glob


working_dir = sys.argv[0].rpartition("/")[0]
inpath = 'D:\PythonCodes\inputmultifiles'
outpath = 'D:\PythonCodes'
pattern = "*.csv"
os.chdir(inpath)
 
def main(inpath,outpath):
    if __name__ == '__main__':
        print("File path",inpath)
        #os.chdir(inpath)
        flist =[]
        for f in (glob.glob(pattern)):
            flist.append(f.split('_')[0])
            print(flist)
        eqp_list = sorted(set(flist))
        #eqp_list=list(set(flist))
        print("The eqplist",eqp_list)
        print(os.getcwd())
#%%
    for fname in eqp_list:
        print(fname)
        print("infiles path",inpath)
        print(os.getcwd())

        print("-"*12)
        fname= fname+ "_SR.csv"
        data.read_data(inpath =inpath,inFile = fname, colNames ='col1', mode = 'NOM', delimiter = ",")
        
main(inpath,outpath)
read_data.py



import pandas as pd
import logging
import os
import sys
from datetime import datetime

 
class MyData:  
    def __init__(self):  
        self.mode = None
       
         
    def read_data(self, inpath,inFile, colNames, mode, delimiter):
        self.infile = inFile
        print("The infile name",inFile)
        print("The inpath name",inpath)
        self.logname = inFile.split('.csv')[0]        
        logging.basicConfig(level=logging.DEBUG,format='%(asctime)s.%(msecs)04d:%(levelname)s:%(lineno)d\
        %(message)s',filename=(self.logname + datetime.now().strftime('%Y%m%d_%H%M%S%f')+".txt"),
                    filemode='w+',datefmt='%Y-%m-%d %H:%M:%S') 

        logging.debug("Hello new log")
        logging.debug("Initialize file read...")
        try:
            self.df = pd.read_csv(inFile)
            print(self.df.head(2))
            output_file = os.path.join(inpath,inFile)
            self.df.to_csv(output_file,index=False)
            logging.debug("Read data successfully")
            print("Read data successfully")
        except OSError as e:
            logging.debug("File read fail check fail reason below")
            logging.debug(e.errno)
         

data = MyData()
my csv:

A_SR.csv


Name  age  eID
AA    23   522
SSG   45   125
AGB    7   4152

B_SR.csv

Name  age  eID
AK    23   545
BJK   45   895
FGGB  7    202



RE: unable to write to log file - Larz60+ - Aug-12-2020

Quote:I wiant to write log file for each loop separatly.
please elaborate on 'each loop'


RE: unable to write to log file - Mekala - Aug-12-2020

I want to write log file for each loop separatly.--> not necesssary. But now it is not even writing any log file.