目標是遍歷文件夾,輸出不含目標文件類型的文件夾路徑;
使用os.walk()函數(shù)
Windows系統(tǒng)中使用os.path.join(),出現(xiàn)\\,使用Path(filePath).as_posix()來處理;
import os
from pathlib import Path
rootDir = 'D:\SVN'
sempty = set([])
slost = set([])
# 判斷沒有目標文件類型的文件夾
def lost_file(medimfour):
a = 0
for file in medimfour:
if os.path.splitext(file)[1].find('mp4') == 1:
a += 1
if a == 0:
return True
else:
return False
def list_all(vedio):
global sempty, slost
if not os.listdir(vedio):
sempty.add(Path(vedio).as_posix())
else:
for root, dirs, files in os.walk(vedio):
if len(dirs) == 0 and len(files) == 0:
sempty.add(Path(root).as_posix())
if len(dirs) == 0 and len(files) > 0 and lost_file(files):
slost.add(Path(root).as_posix())
list_all(rootDir)
print('空文件夾', len(sempty), sempty)
print('lost文件', len(slost), slost)