简体   繁体   English

尝试将具有特定扩展名的文件从源文件夹复制到目标文件夹

[英]Trying to copy files with a specific extension from source folder to destination folder

I am testing an idea to copy all files with the extension '.yaml' from one folder (and all sub-folders) to another folder.我正在测试将所有扩展名为“.yaml”的文件从一个文件夹(和所有子文件夹)复制到另一个文件夹的想法。 I came up with the code below.我想出了下面的代码。

import os
import shutil

src = 'C:\\Users\\ryans\\OneDrive\\Documents\\GitHub\\Pipeline\\'
dest = 'C:\\Users\\ryans\\OneDrive\\Desktop\\AllYAML\\'

#src_files = os.listdir(src)

for root, dirs, files in os.walk(src):  
    for filename in files:
        full_file_name = os.path.join(src, filename)
        if (os.path.isfile(full_file_name)):
            if full_file_name.endswith('.yaml'):
                shutil.copy(full_file_name, dest)

This seems right, for the most part, but it is only copying over two files with the extension '.yaml' from source to destination.这在大多数情况下似乎是正确的,但它只是将扩展名为“.yaml”的两个文件从源复制到目标。 There are around 130 files in all folders and sub-folders, so I know something is off here, but I don't know quite what the issue is.所有文件夹和子文件夹中大约有 130 个文件,所以我知道这里出了点问题,但我不太清楚问题是什么。

You should join the path of the file with the file name instead of joining the starting path with the file name.您应该将文件路径与文件名连接起来,而不是将起始路径与文件名连接起来。

Change:改变:

full_file_name = os.path.join(src, filename)

to:到:

full_file_name = os.path.join(root, filename)

this should copy all the .yaml files from src folder and its sub-folders into dest folder, keeping in mind the src sub-folder structure will NOT BE maintained in dest.这应该将 src 文件夹及其子文件夹中的所有 .yaml 文件复制到 dest 文件夹中,记住 src 子文件夹结构不会在 dest 中维护。

import os
import shutil

src = 'C:\\Users\\ryans\\OneDrive\\Documents\\GitHub\\Pipeline\\'
dest = 'C:\\Users\\ryans\\OneDrive\\Desktop\\AllYAML\\'

#src_files = os.listdir(src)

for root, dirs, files in os.walk(src):
    for filename in files:
        full_file_name = os.path.join(root, filename)
        if (os.path.isfile(full_file_name)):
            if full_file_name.endswith('.yaml'):
                shutil.copy(full_file_name, dest)

Mainly filename is to be joined with respective directory to which it belongs like full_file_name = os.path.join(root, filename)主要是文件名要与它所属的各个目录连接,如 full_file_name = os.path.join(root, filename)

暂无
暂无

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

相关问题 将文本文件中列出的特定文件从源文件夹复制到目标文件夹 - Copy the specific files listed in text file from source folder to Destination folder 将指定数量的文件从源文件夹移动到目标文件夹 - Move specified number of files from source folder to destination folder 将文件从源文件夹复制到其他目标文件夹 - Copying files from source folder to different destination folders 使用面料复制具有特定扩展名的文件夹 - Copy folder with specific extension with fabric 在SVN中的目录中搜索具有特定文件扩展名的文件,然后复制到另一个文件夹? - Search directory in SVN for files with specific file extension and copy to another folder? 如果字符串等于文件文件夹,则将文件复制到目标文件夹 - Copy Files to Destination Folder if String Equals File Folder 如何复制具有特定扩展名的文件夹目录中的文件,然后将其保存到新文件夹中而不覆盖已复制的文件? - How do I copy files in a folder directory with a specific extension and save it to a new folder with out overwriting copied files? 有没有办法将随机文件从源复制到目标? - Is there a way to copy random files from source to destination? 将文件从文件夹复制到文件夹python - copy files from folder to folder python 使用shutil.copy复制文件,匹配某些年龄和扩展名,并且树文件夹模式与源文件相同 - Copying files with shutil.copy matching some age and extension with same tree folder pattern as source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM