leon1912
leon1912

Reputation: 1

Python - How can the script go through directories and check if there are empty or not?

I have an exercise to walk through all direcories in a subdirectory and check if there are empty or not. I already tried something but this don't work:

import os
path = input(">>> ")

for (root,dirs,files) in os.walk(path, topdown=True):
    if len(dirs and files) == 0:
        print(os.listdir(dirs)) == 0
        print(root)
        print(dirs)
        print(files)
        print('--------------------------------')
    else:
        print()

Can you help me with it?

Thanks

Upvotes: 0

Views: 476

Answers (1)

AlexFocus
AlexFocus

Reputation: 26

You can try this:

'''
    Check if a Directory is empty :
'''    
if len(os.listdir(path) ) == 0:
    print("Directory is empty")
else:    
    print("Directory is not empty")
  • You have to import os

Upvotes: 1

Related Questions