How to check whether a file is empty or not How to check whether a file is empty or not python python

How to check whether a file is empty or not


>>> import os>>> os.stat("file").st_size == 0True


import os    os.path.getsize(fullpathhere) > 0


Both getsize() and stat() will throw an exception if the file does not exist. This function will return True/False without throwing (simpler but less robust):

import osdef is_non_zero_file(fpath):      return os.path.isfile(fpath) and os.path.getsize(fpath) > 0


matomo