
正文
Python3 指定文件夹下所有文件(包括子目录下的文件)拷贝到目标文件夹下
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
#!/usr/bin/env python3
# -*- coding:utf8 -*-
# @TIME :2018/9/17 9:02
# @Author:dazhan
# @File :copyfiles2dir.pyimport os
import shutilsource_path = os.path.abspath(r'Z:\webscraping\output\dazhan_group_WS1810222337649')
target_path = os.path.abspath(r'D:\Weekly_Refresh\needToCheck')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:
src_file = os.path.join(root, file)
shutil.copy(src_file, target_path)
print(src_file)print('copy files finished!')







