简体   繁体   English

将多个文件从子文件夹复制到一个文件夹

[英]Copy multiple files from sub folder into one folder

I have folder that has subfolders each with many different files. 我有一个包含子文件夹的文件夹,每个子文件夹包含许多不同的文件。 I'd like to copy all of the files (not subdirectories) into one folder 我想将所有文件(不是子目录)复制到一个文件夹中

import os
import shutil

src = r'C:\TEMP\dir'
dest = r'C:\TEMP\new'

src_files = os.listdir(src)
for file_name in src_files:
    full_file_name = os.path.join(src, file_name)
    if (os.path.isfile(full_file_name)):
        shutil.copy(full_file_name, dest)

when I run the code, there is no error but no files is copied either. 当我运行代码时,没有错误,但是也没有文件被复制。 I do not know what is wrong with the code. 我不知道代码有什么问题。

You can try this 你可以试试这个

import os
import shutil

src = r'C:\TEMP\dir'
dest = r'C:\TEMP\new'

for path, subdirs, files in os.walk(src):
    for name in files:
        filename = os.path.join(path, name)
        shutil.copy2(filename, dest)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件从一个文件夹复制到另一个文件夹(而不是覆盖) - Copy files from one folder to other (and not overwrite) 如何将奇数文件从一个文件夹复制到另一个文件夹? - How to copy odd numbered Files from one folder to another folder? 如何从文件夹到文件夹复制一百个文件? - How to copy one hundred files from folder to folder? 如何在不复制python中的文件夹结构的同时从文件夹(包括子文件夹)复制所有文件 - How to copy all files from a folder (including sub-folder) while not copying the folder structure in python 使用python脚本将一个文件夹中的多个jpeg文件和json文件复制并排序到子文件夹文件夹中的另一个文件夹 - copy and sort multiple jpeg files and json files in one folder to another folder in subfolders folder using python script 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 将文件从一个 AWS s3 存储桶/文件夹复制到另一个 AWS/S3 文件夹,并通过 python 在数据块上保留最深的子文件夹名称 - copy files from one AWS s3 bucket/folder to another AWS/S3 folder and also keep the deepest sub-folder name by pythons on databricks 如何使用python将多个文件夹中的多个文件复制到一个文件夹中? - How to copy multiple files from multiple folders into one folder using python? 将服务器文件夹中的多个文件合并为一个 - Merge multiple files from server folder into one 将文件从一个文件夹复制到另一个文件夹并重命名文件 - Copy files from one folder to another and rename the files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM