0

I am trying to empty a log file created in logging.ini as shown below, with this approach it clears the contents but the empty spaces of the contents still exist and then the new contents will be appended to it.

[handler_filehandler]
class=FileHandler
level=DEBUG
formatter=json
args=("../logs/test_log.log",'w')
fn_path = os.path.join(os.path.dirname(__file__),
                       '..',
                       'logs',
                       'test_log.log')
fn_path = os.path.abspath(fn_path)

with open(fn_path, 'w'):
   pass

Have attached a image below of log when I open it in notepad

it shows empty contents on the second run of my application at first run there are no empty line, i clear the log once the first run is done , on the second run the logs are added after empty line

How can I clear all the contents without any empty spaces left in log file enter image description here

shee
  • 165
  • 1
  • 10
  • Please share an example. What do you mean by "the empty spaces of the contents still exist"? The flags - `w`/`a` are handled by the operating system, not Python. – Daniel Trugman Sep 07 '21 at 10:29
  • @DanielTrugman Have edited my post please check – shee Sep 07 '21 at 10:50
  • How do you open the file? Are you sure it's not a different view? What OS are you running on? – Daniel Trugman Sep 07 '21 at 10:51
  • Opening it with notepad in windows OS – shee Sep 07 '21 at 10:53
  • 1
    Does your solution append empty lines? Did you try `with open(..., "w") as f: f.write("")`? What happens then? – André Sep 07 '21 at 10:56
  • Looks like python is not directly calling the OS's implementation - https://stackoverflow.com/questions/8945370/garbage-in-file-after-truncate0-in-python/46384466 – Daniel Trugman Sep 07 '21 at 10:57
  • No this happens on the second run of my application at first run there are no empty line, i clear the log once the first run is done , on the second run the logs are added after empty line – shee Sep 07 '21 at 10:58
  • Dear @she Please do not add informations as comments. Edit your own question please. – buhtz Sep 07 '21 at 11:08
  • What library are you using that takes that INI file? And where in your code do you actually use it? – OneCricketeer Sep 07 '21 at 11:51
  • 1
    @OneCricketeer logging and logging.config – shee Sep 07 '21 at 11:53
  • Okay, so can you show where you are importing the logging library and loading the file in comparison to where you're trying to clear the file? Also, I'd expect `args=("../logs/test_log.log",'w')` to be clearing the file itself, not appending to previous logs – OneCricketeer Sep 07 '21 at 11:58

0 Answers0