[关闭]
@bergus 2015-12-02T06:38:29.000000Z 字数 767 阅读 2091

python shutil模块

python python模块 shutil


  1. # encoding=utf-8
  2. # /home/bergus/tongbu/360共享/编程语言
  3. # /home/bergus/桌面
  4. # /home/bergus/test/hh
  5. import os
  6. import shutil
  7. # 把文件src_file移动到目录dest_file
  8. def move(src_file, dest_file):
  9. for src in src_file:
  10. for dest in dest_file:
  11. try:
  12. shutil.move(src, dest)
  13. except Exception, e:
  14. print e
  15. # 得到关键字的路径
  16. def get_keyworld_paths(key_word, path):
  17. dest_dirs = []
  18. if os.path.exists(path):
  19. for f in os.listdir(path):
  20. # print f
  21. if key_word in f.lower():
  22. dest_dirs.append(os.path.join(path, f).replace('\\', '/'))
  23. return dest_dirs
  24. def my_move():
  25. src_dir = '/home/bergus/桌面'
  26. dest_dir = '/home/bergus/tongbu/360共享/编程语言'
  27. key_word = ['python', 'flask', 'sqlalchemy']
  28. for key in key_word:
  29. try:
  30. a1 = get_keyworld_paths(key, src_dir)
  31. a2 = get_keyworld_paths(key, dest_dir)
  32. move(a1, a2)
  33. print "移动完成"
  34. except Exception, e:
  35. print e
  36. if __name__ == '__main__':
  37. my_move()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注