zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    leetcode 443: String Compression,357: Count Numbers with Unique Digits
    C++ 中 freopen()函数的用法
    filter
    map
    os.listdir
    os.path.join
    assert
    numpy中的axis和Pytorch中的dim参数
    mac中qq接收视频存放的位置
    requests
  • 原文地址:https://www.cnblogs.com/bergus/p/4519403.html
Copyright © 2011-2022 走看看