Skip to content
Advertisement

Check the presence of files within directory

under “mypath” i have one folder and under this folder i have files and i want to check the existance of files under this folder, but in my example below it checks only the directory where is the folder (where there is no files) , So any idea?

for root, dirname , file in os.walk(mypath)):
    if not file:
        raise Exception("there is no file")

Advertisement

Answer

You need to check the right directory:

for root, dirname , file in os.walk('c:\myfolder', topdown=False):
    if root != 'c:\myfolder' and not file:
        raise Exception("there is no file")
    elif root != 'c:\myfolder':
        break

Like @tdelaney’s answer, does not check if there are multiple subdirectories

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement