check multiple files if they are empty in python
I have an n number of log files that a script regularly downloads and upload on slack for monitoring purposes. However with recent improvements in our postgresql database some of the log files are now empty (meaning no errors or long queues were recorded) this being said, I would need to segregate files that are empty vs not empty and if it's empty skip the file from being uploaded entirely and proceed with the ones that are not empty.
348 postgresql.log.2021-09-28-0000
679 postgresql.log.2021-09-28-0100
0 postgresql.log.2021-09-28-0200
0 postgresql.log.2021-09-28-0300
0 postgresql.log.2021-09-28-0400
0 postgresql.log.2021-09-28-0500
0 postgresql.log.2021-09-28-0600
0 postgresql.log.2021-09-28-0700
0 postgresql.log.2021-09-28-0800
0 postgresql.log.2021-09-28-0900
0 postgresql.log.2021-09-28-1000
0 postgresql.log.2021-09-28-1100
0 postgresql.log.2021-09-28-1200
0 postgresql.log.2021-09-28-1300
0 postgresql.log.2021-09-28-1400
0 postgresql.log.2021-09-28-1500
0 postgresql.log.2021-09-28-1600
0 postgresql.log.2021-09-28-1700
0 postgresql.log.2021-09-28-1800
0 postgresql.log.2021-09-28-1900
0 postgresql.log.2021-09-28-2000
0 postgresql.log.2021-09-28-2100
0 postgresql.log.2021-09-28-2200
0 postgresql.log.2021-09-28-2300
In this case we can see that only the files
348 postgresql.log.2021-09-28-0000
679 postgresql.log.2021-09-28-0100
contains 348 bytes and 679 bytes of data respectively.
How do I make it so that the python script that is being used right now would validate if the file is empty first before being uploaded?
The closest thing I have found right now is
import os
if os.stat("postgresql.log.2021-09-28-2300").st_size == 0:
print('empty')
but this only checks for one file at a time, and I would rather do it in the whole directory as the file names (Date in particular and time) would change.
I'm relatively new at this and this was just handed down to me at work and I'd appreciate guides on how to make it work thank you so much.
from Recent Questions - Stack Overflow https://ift.tt/2XYodhK
https://ift.tt/eA8V8J
Comments
Post a Comment