DEV Community

Coding Panel
Coding Panel

Posted on • Originally published at kodlogs.net

Python check if generator is empty

You can "Python check if generator is empty" by using multiple methods and approaches. It has a set of built-in library objects and functions to help us with this task. In this tutorial, we will learn how to check if a file or directory is empty in Python.
Image description

What is "Python check if generator is empty"?

Python is a scripting language that has made it extremely easy to create new programming languages. Python allows us to develop many different kinds of programs, including bytecode based interpreters, applications and libraries. Basically, python generators are the simplest way to create iterators.

Why "Python check if generator is empty"?

When we would like to check if a path is empty or not, we will want to know if it is a file or a directory, as this affects the approach we want to use. Let's say we have two placeholder variables dirpathand filepathidentifying a local directory and file:

dirpath="/mnt/f/code.books/articles/python"
filepath="/mnt/f/code.books/articles/python/code/file_dir.py"
Enter fullscreen mode Exit fullscreen mode

Solution 1: Using os.path
Python provides the osmodule which is a standard Python package of functions, objects, and constants for working with the operating system.

os.pathprovides us with the isfile()and isdir()functions to effortlessly distinguish between a file and the given directory:

import os

dirpath="/mnt/f/code.books/articles/python"
filepath="/mnt/f/code.books/articles/python/code/file_dir.py"

os.path.isfile(dirpath) # False
os.path.isdir(dirpath) # True
os.path.isfile(filepath) # True
os.path.isdir(filepath) # False

Enter fullscreen mode Exit fullscreen mode

Both functions return a Booleanvalue.

Solution 2: Using pathlib
Python 3.4 introduced the pathlibmodule, which provides an object-oriented interface for working with file systems.

pathlibsimplifies working with file systems compared to osor os.path.

The Pathclass of the pathlibmodule accepts a path as an argument and returns an Pathobject, which can be easily queried or chained with methods and attributes:

from pathlib import Path

dirpath="/mnt/f/code.books/articles/python"
filepath="/mnt/f/code.books/articles/python/code/file_dir.py"

Path(dirpath).is_file() # False
Path(dirpath).is_dir() # True
Path(filepath).is_file() # True
Path(dirpath).is_file() # False

Enter fullscreen mode Exit fullscreen mode

Here, we are checking if Paththe object is a file or directory instead.

Check if a file is empty
An empty file or zero-byte file is any file that contains no data or content. The file can be of any type. Some files (such as music files) may have no data, but still contain metadata (such as author). These files cannot be considered as an empty file.

An empty file can be created quickly on Linux and MacOS:

$ touch emptyfile
Or on Windows:

$ type nul > emptyfile
Let's define variables now – emptyfileand nonemptyfilepointing to an empty file that is zero bytes and a non-empty file that is one byte in size:

emptyfile="/mnt/f/code.books/articles/python/emptyfile"
nonemptyfile="/mnt/f/code.books/articles/python/onebytefile"
Enter fullscreen mode Exit fullscreen mode

You can take a look at the types and sizes of given files:

$ ls -l
-rwxrwxrwx 1 root root 0 Sep 10 18:06 emptyfile
-rwxrwxrwx 1 root root 1 Sep 10 18:08 onebytefile
$ file emptyfile
emptyfile: empty
$ file onebytefile
onebytefile: very short file (no magic)

There is also another solution which uses os.stat. You can read more about that here

Top comments (0)