利用python copy目录下全部特定后缀的文件

python 太好用了html

这一次我想将子目录先全部jpg和pdf文件都copy出来放到一个文件夹,在网上找了个copy所有文件的代码修改一下就搞定了python

 

import os import shutil source_path = os.path.abspath(r'F:\tool\') target_path = os.path.abspath(r'D:\putout') if not os.path.exists(target_path): os.makedirs(target_path) if os.path.exists(source_path): # root 所指的是当前正在遍历的这个文件夹的自己的地址
    # dirs 是一个 list,内容是该文件夹中全部的目录的名字(不包括子目录)
    # files 一样是 list, 内容是该文件夹中全部的文件(不包括子目录)
    for root, dirs, files in os.walk(source_path): for file in files: if file.find('jpg') > -1 or  file.find('jpeg') > -1 or  file.find('pdf') > -1: src_file = os.path.join(root, file) shutil.copy(src_file, target_path) print(src_file) print('copy files finished!')

 

参考代码 https://www.cnblogs.com/dazhan/p/9834979.htmlspa

 

不过作文件类型判断不严谨,应该用filetypecode

(前不久还说不发随笔了,打脸)htm

相关文章
相关标签/搜索