zoukankan      html  css  js  c++  java
  • 根据txt中的文件名将文件复制到目标文件夹中

    功能如标题,之所以这么做是有的时候文件数目较多,一个一个复制太复杂了,代码如下:

    # -*- coding:utf-8 -*-
    #2018_03_18
    #实现功能:根据文件名字将对应的文件复制到目标地址中,之所以这么做是由于有的时候文件名太多了,一个一个复制很麻烦
    #===========================================================================import 
    import os
    import shutil
    
    #根据txt_path对应的txt中的文件名将文件一个一个复制到save_path中,name_extension是后缀名
    def search_by_name(original_path , txt_path , save_path , name_extension):
    
        if(os.path.exists(txt_path) == False):
            print "txt_path not exist!"
            return
    
        f = open(txt_path , 'r') 
    
        lines = f.readlines()
        f.close()
    
        flies_count = 0  #总共要进行的文件数目
    
        for i in range(len(lines)):
            files = lines[i].split()  #文江名之间使用空格、
    分离
    
            for j in range(len(files)):
                file_name = os.path.splitext(files[j])[0]  #提取文件名,不包括后缀名
                file_full_name = file_name + name_extension
                print file_full_name
                file_full_path = original_path + file_full_name  #源文件的路径
    
                if(os.path.exists(file_full_path) == False):
                    print file_full_path + "not exist!"
                    continue
    
                shutil.copyfile(file_full_path , save_path + file_full_name)
            flies_count += len(files)
        print flies_count #输出总共执行的文件数目
    
    if __name__ == '__main__':
    
        original_path = 'D:/projects/darknet-master/src/'  #源文件夹路径
        txt_path = 'C:/Users/zf/Desktop/txt_path.txt'  #依据的txt文件
        save_path = 'D:/projects/darknet-master/yolo9000/h/'  #将要复制到的目标文件夹
        name_extension = '.h'  #后缀名
        search_by_name(original_path , txt_path , save_path , name_extension)
  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/zf-blog/p/8595303.html
Copyright © 2011-2022 走看看