python 练习 : 帮我把硬盘里的图片都找出来
要点:使用系统模块,遍历磁盘,遍历文件,找到符合条件的文件记录下来到一个Log文件中(XML格式).python 代码
[*]import win32api
[*]import os
[*]
[*]#constants and configs
[*]IGNORE_PATH = [
[*] "C:\\WINDOWS",
[*] "C:\\Program Files",
[*] "C:\\Documents and Settings",
[*] "C:\\System Volume Information"
[*] ]
[*]
[*]#TODO use re replace
[*]IMAGE_TYPE = [
[*] "jpg","gif","png","jpeg","bmp"
[*] ]
[*]#find out the logic disks
[*]
[*]disks = win32api.GetLogicalDriveStrings().split("\x00")
[*]for disk in disks:
[*] if disk in ['','A:\\']:
[*] disks.remove(disk)
[*]
[*]#TODO store to xml file
[*]def store(filePath,fileName):
[*] print 'about to store %s\\%s' % (filePath,fileName)
[*]
[*]def scan(path):
[*] print 'scanding path : ' + path
[*] if not path.endswith('\\'):
[*] path = path + '\\'
[*] for _file in os.listdir(path):
[*] if os.path.isdir(path + _file) and path + _file not in IGNORE_PATH:
[*] scan(path + _file)
[*] else:
[*] for image_type in IMAGE_TYPE:
[*] if _file.endswith(".%s" % image_type):
[*] store(path,_file)
[*] break
[*]
[*]# now iterat the disks
[*]for disk in disks:
[*] print 'now scan disk : %s ' % disk
[*] scan(disk)
到此为止,Python把硬盘里的图片都找出来了,还差把图片信息存储到XML文件里面去.
TODOs:
一,使用正则表达式匹配后缀名
二,保存信息到XML
页:
[1]