Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to write to log file
#1
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
Reply
#2
Quote:I wiant to write log file for each loop separatly.
please elaborate on 'each loop'
Reply
#3
I want to write log file for each loop separatly.--> not necesssary. But now it is not even writing any log file.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to capture all images of a multipage TIFF file in a merge bendersbender 0 64 Nov-19-2020, 03:09 PM
Last Post: bendersbender
  How do I write a single 8-bit byte to a file? MysticLord 2 296 Sep-03-2020, 12:27 PM
Last Post: MysticLord
  Unable to Validate csv blanck data and write in csv prashant18 0 273 Jul-25-2020, 12:08 PM
Last Post: prashant18
  List Won't Write in Text File IILawrenceII 4 398 Jul-17-2020, 11:16 PM
Last Post: j.crater
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 371 Jun-17-2020, 06:00 PM
Last Post: buran
  Write SQLite query result to file hjk6734 1 349 May-27-2020, 12:17 PM
Last Post: menator01
  Append JSON's and write to file faqsap 4 560 May-15-2020, 04:20 PM
Last Post: faqsap
  Print/write to file function tpolim008 4 545 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  Write object to file medatib531 7 611 Apr-01-2020, 06:25 PM
Last Post: BrendanD
  Replace XML tag and write to the same file krish216 4 614 Mar-26-2020, 07:39 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020