Traverse all subordinate files and folders in the directory, including subfolders, to find empty files and empty folders and delete them
def Clean_empty(path):
"""
Iterate through all subfolders and subfiles under a file, cleaning up empty folders and files
path:file path
"""
for (dirpath,dirnames,filenames) in os.walk(path):
for filename in filenames:
file_folder=dirpath+'/'+filename
# print(file_folder)
if os.path.isdir(file_folder):
if not os.listdir(file_folder):
print(file_folder)
# os.rmdir(dirpath+filename)
elif os.path.isfile(file_folder):
if os.path.getsize(file_folder) == 0:
print(file_folder)
os.remove(file_folder)
print(path, 'clean over!')
if __name__ == "__main__":
path = '/data/git/ocr-platform/data/annotation_data/recognize/dataset/ocr_dataset_etc'
Clean_empty(path)
Done!