python 文件移动(shutil)
# encoding=utf-8 # /home/bergus/tongbu/360共享/编程语言 # /home/bergus/桌面 # /home/bergus/test/hh import os import shutil # 把文件src_file移动到目录dest_file def move(src_file, dest_file): for src in src_file: for dest in dest_file: try: shutil.move(src, dest) except Exception, e: print e # 得到关键字的路径 def get_keyworld_paths(key_word, path): dest_dirs = [] if os.path.exists(path): for f in os.listdir(path): # print f if key_word in f.lower(): dest_dirs.append(os.path.join(path, f).replace('\\', '/')) return dest_dirs def my_move(): src_dir = '/home/bergus/桌面' dest_dir = '/home/bergus/tongbu/360共享/编程语言' key_word = ['python', 'flask', 'sqlalchemy'] for key in key_word: try: a1 = get_keyworld_paths(key, src_dir) a2 = get_keyworld_paths(key, dest_dir) move(a1, a2) print "移动完成" except Exception, e: print e if __name__ == '__main__': my_move()
作者:百里求一
出处:http://www.cnblogs.com/bergus/
我的语雀: https://www.yuque.com/barry.bai
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。