verify that a file is not empty in python

To verify that a file is not empty using Python, you can perform the following steps:

  1. Check if the file exists
  2. Check the size of the file

You can use the os.path.isfile function to check if the file exists and os.path.getsize function to get the size of the file. Here's some sample code that demonstrates how to verify that a file is not empty:

main.py
import os

def is_file_empty(file_path):
    if os.path.isfile(file_path):
        return os.path.getsize(file_path) > 0
    else:
        return False
152 chars
8 lines

To use this function, simply pass in the file path as a parameter:

main.py
if is_file_empty('/path/to/file.txt'):
    print('File is not empty')
else:
    print('File is empty')
103 chars
5 lines

related categories

gistlibby LogSnag