首页 > 代码库 > python学习记录第五篇--遍历目录

python学习记录第五篇--遍历目录

#coding=utf-8
‘‘‘
@author: 
简单遍历目录删除文件的小程序
‘‘‘
import os
#查找文件操作
def findFile(path):
fileList=[]
for rootPath,subRoot,fileName in os.walk(path):
for sub in fileName:
if os.path.isfile(os.path.join(rootPath,sub)):
k=os.path.splitext(sub)[1].lower()
if k in (‘.jpg‘,‘.png‘,‘.gif‘):
j=os.path.join(rootPath,sub)
fileList.append(j)
return fileList

#删除文件操作
def deleteFile(file):
count=0
for s in file:
os.remove(s)
count+=1
print "共%s张图片被删除"% count

if __name__=="__main__":
path =raw_input("请输入要删除图片的根目录,例如,E:\python\pest:\n")
p=findFile(path)
deleteFile(p)