I\'ve searched everywhere for this answer but can\'t find it.
I\'m trying to come up with a script that will search for a particular subfolder then check if it conta
If you can delete the directory, you can use this:
my_path = os.path.abspath("something")
try:
os.rmdir(my_path)
is_empty = True
# Do you need to keep the directory? Recreate it!
# os.makedirs(my_path, exist_ok=True)
except OSError:
is_empty = False
if is_empty:
pass
The os.rmdir only removes a directory if it is empty, otherwise it throws the OSError exception.
You can find a discussion about this on:
For example, deleting an empty directory is fine when you are planing to do a git clone, but not if you are checking beforehand whether the directory is empty, so your program does not throw an empty directory error.