zoukankan      html  css  js  c++  java
  • Python批量拷贝文件

     批量拷贝文件:batchcopy.py

    def batchcopyimgs(dir_orig, dir_desc, file_type):
        cwd = os.getcwd()
        path_orig = os.path.join(cwd, dir_orig)
        path_desc = os.path.join(cwd, dir_desc)
        if not os.path.exists(path_orig):
            print("path_orig is not exist!")
            raise
    
        if not os.path.exists(path_desc):
            os.makedirs(path_desc)
    
        #for parent, dirname, filenames in os.walk(path_orig):
        #    print("
    parent:", parent, " dirname:", dirname, " filenames:", filenames)
        file_list_1 = os.listdir(path_orig)
        for file in file_list_1:
    
            camera_id = file.split("_")[-1]
            filepath = os.path.join(path_orig, file)
            if os.path.isdir(filepath):
                file_list_2 = os.listdir(filepath)
                for file2 in file_list_2:
                    filepath2 = os.path.join(filepath, file2)
                    if filepath2.endswith(file_type):
                        #print("copying", filepath2)
                        new_name = "C"+camera_id + "_" + dir_orig + "_" + file2
                        path_new_desc = os.path.join(path_desc, new_name)
                        print("copying path_new_desc:", path_new_desc)
                        shutil.copy(filepath2, path_new_desc)
                sleep(0.1s)
    else: print("not directory,continue...") def main(): dir_desc = "JPEGImages" dir_orig = "data_acquisition" file_type = ".jpeg" batchcopyimgs(dir_orig, dir_desc, file_type) if __name__ == "__main__": main()

    注意点1:

     批量拷贝文件时,如果需要对拷贝的文件重命名,只需在目标路径中指定上新文件名,如目标路径中只是文件夹,则为同名拷贝

    shutil.copy(filepath_orig, filepath_desc)

    注意点2:

     如果直接使用shutil.copy(filepath2, path_new_desc),由于拷贝需要一定的时间,在拷贝的过程中会出现文件漏拷贝问题

     解决方法:在拷贝文件操作的下面加上sleep(0.1) ,来保证拷贝过程正常完成,注:括号里的数据为ms级,可根据拷贝操作需要的时间自行调节

    人生,从没有一劳永逸 想要变强,只有不停奔跑
  • 相关阅读:
    [Unity] 2D开发学习教程
    [Unity] 查找资源
    [Unity] UGUI研究院之游戏摇杆
    [Unity] Unity3D研究院编辑器之自定义默认资源的Inspector面板
    [Unity] Unity3D研究院编辑器之独立Inspector属性
    [Unity] 精灵动画制作中需要注意的一些问题
    [Unity] 常用技巧收集
    IDEA相关设置
    Hive配置文件hive-site.xml
    MySql通用二进制版本在Linux(Ubuntu)下安装与开启服务
  • 原文地址:https://www.cnblogs.com/jimchen1218/p/14477774.html
Copyright © 2011-2022 走看看