Python3 指定文件夾下所有文件(包括子目錄下的文件)拷貝到目標文件夾下


#!/usr/bin/env python3
# -*- coding:utf8 -*-
# @TIME  :2018/9/17 9:02
# @Author:dazhan
# @File  :copyfiles2dir.py

import os
import shutil

source_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!')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM