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
  write mariadb table rows query to each file? shams 1 277 Feb-02-2021, 04:10 PM
Last Post: buran
  Read/Write binary file deanhystad 3 413 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 636 Jan-14-2021, 08:58 PM
Last Post: snippsat
  Unable to capture all images of a multipage TIFF file in a merge bendersbender 0 296 Nov-19-2020, 03:09 PM
Last Post: bendersbender
  How do I write a single 8-bit byte to a file? MysticLord 2 641 Sep-03-2020, 12:27 PM
Last Post: MysticLord
  Unable to Validate csv blanck data and write in csv prashant18 0 452 Jul-25-2020, 12:08 PM
Last Post: prashant18
  List Won't Write in Text File IILawrenceII 4 640 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 610 Jun-17-2020, 06:00 PM
Last Post: buran
  Write SQLite query result to file hjk6734 1 611 May-27-2020, 12:17 PM
Last Post: menator01
  Append JSON's and write to file faqsap 4 971 May-15-2020, 04:20 PM
Last Post: faqsap

Forum Jump:

User Panel Messages

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