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
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,367 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,303 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,000 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,499 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,504 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,359 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  Upgrading from 2 to 3 and having file write problems KenHorse 2 1,430 May-08-2022, 09:47 PM
Last Post: KenHorse
  Cursor write 3rd file empty paulo79 3 1,826 Mar-10-2022, 02:51 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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