I have a text file. How can I check whether it's empty or not?
A shell script (isEmpty.sh) example to check if a file is empty or not. echo "$f has data." echo "$f is empty."
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.
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.
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.
>>> import os >>> os.stat("file").st_size == 0 True
import os os.path.getsize(fullpathhere) > 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With