Home > Mobile >  Python - get number of folder in list if file was found
Python - get number of folder in list if file was found

Time:11-17

How i can get number of folder in my list when folder was found? - For example: [Folder1, Folder2, Folder3] I open all folders contained in this list by:

for root, dirs, files in os.walk(path):
    pass

And in this folders i have something to find, example: file name in Folder2 -> xxx.png When i was found this i wanna get get the number of Folder2 in my list i.e 1 ([Folder1, Folder2, Folder3])

CodePudding user response:

Use this example:

import os

# folder path
dir_path = r'E:\account'
count = 0
# Iterate directory
for path in os.listdir(dir_path):
    # check if current path is a file
    if os.path.isfile(os.path.join(dir_path, path)):
        count  = 1
print('File count:', count)
  • Related