title: How to avoid created empty logger file if no valid parsing output in python?
body: <p>I have a python script to run log parsing and it will periodically scan through some log files on the disk and try to parse them. However, If the file is not parsable or have no data, my code should just exit for the parsing.
  The problem is my script generating an empty log file even when there is not a valid data. </p>
  
  <p>ie:
  -rw-r--r-- 1 user userid  0 May 28 08:10 parse.py_20190528_08_10_03.log </p>
  
  <p>I guess this is probably because logger is already initialized when my script is started.
  What I want to know is if there is some other way to avoid this by setting? I tried to check a few places but do not know how.</p>
  
  <p>This is my import logger in my script:</p>
  
  <pre class="lang-py prettyprint-override"><code>import logger
  logger = logging.getLogger('upgrade.py')
  formatter=logging.Formatter("%(asctime)s - %(levelname)-8s %(message)s")
  log_filename = '{}/{}_{}.log'.format(os.getcwd(),os.path.basename(sys.argv[0]),time.strftime("%Y%m%d_%H_%M_%S"))
  fh = logging.FileHandler(log_filename)
  fh.setLevel(logging.INFO)
  fh.setFormatter(formatter)
  logger.addHandler(fh)
  </code></pre>
  
  <p>After my parsing function, I use below to just make sure it did not dump data if no valid data.</p>
  
  <pre class="lang-py prettyprint-override"><code>   main()
       ......parsing....
      if len(outputs) != 0:
          logger.info(outputs)
       .......
       .... output filtering.....
      if len(out_list) == 0:
          exit(0)
       .....
  </code></pre>
  
  <p>However, this still not prevents it is creating 0 kb file in my directory. I trigger this tool in crontab and it is running periodically which generate lots of such files which is annoying and bad to check.
  I know I can also have some outside watcher script to clear those file but that is not a smart act.</p>