0

I am writing some logs in a file using logging module

#filename : demo.py
import logging
#other imports as well
logging.basicConfig(filename="myfile.log",
                    format='%(asctime)s %(funcName)s %(levelname)s %(message)s',
                    filemode='w')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug("Writing logs in this way")

But sometimes my module writes lots of nullvalues

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@

So I have written a script to remove these values. But when I modify myfile.log my further logs are not stored. I know that this is due to change/close of file stream of myfile.log. Is it possible to modify my log file without hurting or losing my already existing stream which demo.py has. I am using ubuntu : 18.04 and python : 3.6.9
For modification I tried the following ways

  1. I am using script sed -i -e 's/\o00//g' myfile.log to modify file
  2. I manually edit file using vim : vim myfile.log To delete line I used dd then closed using :wq

From a comment https://stackoverflow.com/questions/64875452/modify-a-file-without-changing-its-file-stream I know that both method over write new file over old file. Is there a way to edit file inplace.

0 Answers0