python3 判断文件夹内文件数量以及文件夹数量
path = 'D:/test'
print('dirnum:',len([lists for lists in os.listdir(path) if os.path.isdir(os.path.join(path, lists))])) # 文件夹数量
print('filenum:',len([lists for lists in os.listdir(path) if os.path.isfile(os.path.join(path, lists))])) # 文件数量
方法二:
dirnum = 0
filenum = 0
path = 'D:/test'
for lists in os.listdir(path):
sub_path = os.path.join(path, lists)
print(sub_path)
if os.path.isfile(sub_path):
filenum = filenum+1
elif os.path.isdir(sub_path):
dirnum = dirnum+1
print('dirnum: ',dirnum)
print('filenum: ',filenum)