python check if directory empty
Table of Contents
1 requirement
If generated directory is empty, remove it.
2 code sample
To check if the directory is empty, try `os.listdir'
os.listdir(path='.') Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.' and '..' even if they are present in the directory.
Reference: https://docs.python.org/3/library/os.html#os.listdir
Sample code as below:
#!/usr/bin/env python3 import os def check_empty(dir): print(dir + " is : ") if os.listdir(dir): print('not empty') else: print('empty') check_empty('./testdir')