0

I've scheduled my py file and wanted to write today's datetime into seperate log files after the code ran.

Test1:

from datetime import datetime, timedelta
import logging

sheet_list=['A','B','C','D']

def log(sheet):
    today=datetime.today()
    file_name='C:\\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\{}.log'.format(sheet)
    print(file_name)
    logging.basicConfig(filename = file_name,filemode = "w",level = logging.INFO)
    logger = logging.getLogger()
    logging.info(today)
    return ("done!")

for sheet in sheet_list:
    log=log(sheet)

Test1 output:

C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\A.log 

TypeError                                 Traceback (most recent call last)
Cell In[2], line 2 
----> 2 log=log(sheet)

TypeError: 'str' object is not callable

It only printed first sheet's file_name, but failed afterwards. Then I did Test2 to see if the error happened in Test1 was because I didn't do the function right. Although Test2 printed all the file_name, I only found 1 log file in my logs folder, which is A.log. And it records 4 logging info that was supposed to write into different log files.

Test2:

from datetime import datetime, timedelta
import logging

sheet_list=['A','B','C','D']
for sheet in sheet_list:
    today=datetime.today()
    file_name='C:\\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\{}.log'.format(sheet)
    print(file_name)
    logging.basicConfig(filename = file_name,filemode = "w",level = logging.INFO)
    logger = logging.getLogger()
    logging.info(today)

Test2 output:

C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\A.log
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\B.log
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\C.log
C:\Users\ell\AppData\Local\Microsoft\WindowsApps\logs\D.log

A.log:

INFO:root:2023-06-30 11:04:49.034897
INFO:root:2023-06-30 11:04:49.036891
INFO:root:2023-06-30 11:04:49.036891
INFO:root:2023-06-30 11:04:49.036891

I need to know how to modify my code in Test1 to let it write datetime into four seperate log files. Thanks

Lara19
  • 615
  • 1
  • 9
  • 20
  • 1
    After execution `log=log(sheet)` `log` becomes a variable of type `str`. – Yuri Ginsburg Jun 30 '23 at 03:33
  • 1
    Remove `log=` and just call `log(sheet)`. Or, name your variable something other than the name of the method it is calling: e.g. `result=log(sheet)`. In your code, the python interpreter assigns the symbol called `log` the type `str` since it is the return value of the method `log`, but then that symbols is taken and you can't call your method called `log` anymore because the symbol has been reassigned. – dmedine Jun 30 '23 at 03:33
  • @dmedine Thank you, I just did, now it printed all file_name. However, it wrote 4 datetimes to A.log, like what happened in Test2 – Lara19 Jun 30 '23 at 03:40
  • @YuriGinsburg I got it now, I just edit my code by removing log=, however, I've got the same issues that happened in Test2 – Lara19 Jun 30 '23 at 03:41
  • 1
    Sorry, I misread you question so I deleted my answer. There is an SO post that deals with writing to multiple log files: https://stackoverflow.com/questions/47208633/create-multiple-log-files-python – dmedine Jun 30 '23 at 04:09

0 Answers0