Home > Back-end >  skip function if folder is empty
skip function if folder is empty

Time:10-13

I want to read folder name(A/B/C/D) and call the convenable function for each file in Files folder and then process the next file (pass automatically to the next file and function). Now i want to skip the function if the folder is empty how can i add this condition to my code please

Here is my code :

base_path = 'Files/'
for name in os.listdir(base_path):
    path = os.path.join(base_path, name)
    if os.path.isdir(path):
        files = [os.path.join(path, f) for f in os.listdir(path)]
        if name_to_func[path].__code__.co_argcount == len(files):
            name_to_func[path](*files)
        else:
            name_to_func[path](files)
    else:
        name_to_func[path](path)

CodePudding user response:

You can check like this if a directory is empty:

if len(os.listdir(path)) == 0:
    # Directory is empty
else:    
    # Directory is not empty
  • Related