python_统计文件夹下的所有文件夹数目、统计文件夹下所有文件数目、遍历文件夹下的文件

#统计 /home/dir/ 下的文件夹个数
import os
path ="home/dir"
count = 0
for fn in os.listdir(path): #fn 表示的是文件名
        count = count+1
print count
import os
path = os.getcwd()    #获取当前路径
count = 0
for root,dirs,files in os.walk(path):    #遍历统计
      for each in files:
             count += 1   #统计文件夹下文件个数
print count               #输出结果
# 将生成的人脸数据复制到face文件夹中
def copy_face():
    input_dir = '.\\data\\faces'
    output_dir = '.\\data\\only_faces'
    # 新建输出文件夹
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
    for (path, dirnames, filenames) in os.walk(input_dir):
        for filename in filenames:
            if filename.endswith('jpg'):
                img_path = path + '/' + filename
                print(img_path)
                shutil.copy(img_path, output_dir)

猜你喜欢

转载自blog.csdn.net/Gentlemanman/article/details/85992534