---
title: "python 练习 : 帮我把硬盘里的图片都找出来"
author: "我想我是海"
date: "2007-07-17T02:22:00Z"
tags: []
slug: "python"
draft: false
lastmod: "2007-07-17T02:22:00Z"
id: 101716
url: "https://bbmyth.iteye.com/blog/101716"
---

要点:使用系统模块,遍历磁盘,遍历文件,找到符合条件的文件记录下来到一个Log文件中(XML格式).

python 代码

 

01. import win32api
02. import os
03. #constants and configs
04. IGNORE\_PATH = [
05. "C:\\\\WINDOWS",
06. "C:\\\\Program Files",
07. "C:\\\\Documents and Settings",
08. "C:\\\\System Volume Information"
09. ]
10. #TODO use re replace
11. IMAGE\_TYPE = [
12. "jpg","gif","png","jpeg","bmp"
13. ]
14. #find out the logic disks
15. disks = win32api.GetLogicalDriveStrings().split("\\x00")
16. for disk in disks:
17. if disk in \['','A:\\\\']:
18. disks.remove(disk)
19. #TODO store to xml file
20. def store(filePath,fileName):
21. print 'about to store %s\\\\%s' % (filePath,fileName)
22. def scan(path):
23. print 'scanding path : ' + path
24. if not path.endswith('\\\\'):
25. path = path + '\\\\'
26. for \_file in os.listdir(path):
27. if os.path.isdir(path + \_file) and path + \_file not in IGNORE\_PATH:
28. scan(path + \_file)
29. else:
30. for image\_type in IMAGE\_TYPE:
31. if \_file.endswith(".%s" % image\_type):
32. store(path,\_file)
33. break
34. # now iterat the disks
35. for disk in disks:
36. print 'now scan disk : %s ' % disk
37. scan(disk)

到此为止,Python把硬盘里的图片都找出来了,还差把图片信息存储到XML文件里面去.  
TODOs:  
一,使用正则表达式匹配后缀名  
二,保存信息到XML