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()

 

posted @ 2015-05-21 12:40  白云辉  阅读(1439)  评论(0编辑  收藏  举报