0

I have a python script that needs to check if a log file is empty or has logs in it. I use the os.system in order to run bash commands, found this command in order to find if file is empty or not:

if [ -s /tmp/myfile.txt ]
then
     echo "File not empty"
else
     echo "File empty"
fi

I tried just to write this in one line inside the os.system command but i get an error:

sh: -c: line 1: syntax error: unexpected end of file

How can I solve this?

dddd4
  • 21
  • 1
  • 3

1 Answers1

1

To test whether a file is empty, you can use os.stat:

import os

def is_empty(filename):
    return os.stat(filename).st_size == 0
L3viathan
  • 26,748
  • 2
  • 58
  • 81