将相同后缀的所有文件复制到指定的路径下

#1、读取指定目录下的所有文件


import os
import sys  
import os.path  
import shutil 
import fnmatch



 #获取文件后缀名  
def suffix( file, *suffixName ) :  
    array = map( file.endswith, suffixName )  
    if True in array :  
        return True  
    else :  
        return False  

 
def extractFile() :  
#sourceDir= eachFile(filePath) 
 sourceDir="C:\\"    遍历C盘下的所有文件
 targetDir="F:\\storage1"    将复制出来的文件保存到该目录下
 for root, dirs, files in os.walk('C:\\'):
  for name in files:
   if fnmatch.fnmatch(name, '*.exe'):        将C盘下所有exe文件
    sourceFile = os.path.join( root, name )    
    shutil.copy(sourceFile,  targetDir)


   
 
if __name__ == '__main__':
  extractFile()

猜你喜欢

转载自blog.csdn.net/canlandedoushi/article/details/78315458