Check file is empty or not

Python / Work with Files

283

Given Input:


Expected Output:


Use os.stat('file_name').st_size() function to get the file size. if it is 0 then the file is empty.

Program:

import os

size = os.stat("test.txt").st_size
if size == 0:
    print('file is empty')

Output:

file is empty

This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.