ITeye博客

python 练习 : 帮我把硬盘里的图片都找出来

我想我是海 · 2007-07-17 02:22 · python · Markdown

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

python 代码

 

  1. import win32api
  2. import os
  3. #constants and configs
  4. IGNORE_PATH = [
  5. "C:\\WINDOWS",
  6. "C:\\Program Files",
  7. "C:\\Documents and Settings",
  8. "C:\\System Volume Information"
  9. ]
  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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。