Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a file is empty or not

I have a text file. How can I check whether it's empty or not?

like image 957
webminal.org Avatar asked Mar 24 '10 13:03

webminal.org


People also ask

How do you check if a file is empty or not in shell?

A shell script (isEmpty.sh) example to check if a file is empty or not. echo "$f has data." echo "$f is empty."

How check file is empty or not in C++?

The above code works in a simple manner: peek() will peek at the stream and return, without removing, the next character. If it reaches the end of file, it returns eof() . Ergo, we just peek() at the stream and see if it's eof() , since an empty file has nothing to peek at.

How do you check if a file is empty in Python?

If your file was truly empty, with ls -l (or dir on windows) reporting a size of 0, os. path. getsize() should also return 0.

How do I find empty files in a folder?

Delete Empty Files in a Directory In order to delete empty files, we need to perform two steps. First, search all the empty files in the given directory and then, delete all those files. This particular part of the command, find . -type f -empty -print, will find all the empty files in the given directory recursively.


2 Answers

>>> import os >>> os.stat("file").st_size == 0 True 
like image 111
ghostdog74 Avatar answered Sep 20 '22 15:09

ghostdog74


import os     os.path.getsize(fullpathhere) > 0 
like image 21
Jon Avatar answered Sep 19 '22 15:09

Jon