简体   繁体   English

将(文件)名称末尾带有特定文本的文件从一个文件夹复制到另一个文件夹

[英]Copy files with specific text at the end of their (file) names from one folder to other folder

I have 23000 files in one folder and want to copy files with their names ending with some specific text to other folder using python. I used the following code, though it runs successfully but it doesn't copy the file in the destination folder.我在一个文件夹中有 23000 个文件,并且想使用 python 将名称以某些特定文本结尾的文件复制到其他文件夹。我使用了以下代码,虽然它运行成功但它不会将文件复制到目标文件夹中。

import os
import shutil
path = "D:/Snow_new/test"
outpath = "D:/Snow_new/testout"
for f in os.listdir(path):
    if f.endswith("clip_2"):
        shutil.copyfile(os.path.join(path, f), outpath)

you are trying to copy files to a single filename again and again, so to fix it you will have to add the filename with outpath for every file您正在尝试一次又一次地将文件复制到单个文件名,因此要修复它,您必须为每个文件添加带有输出路径的文件名

import os
import shutil
path = "D:/Snow_new/test"
outpath = "D:/Snow_new/testout"

if not os.path.isdir(outpath):
    os.mkdir(outpath)
else:
    shutil.rmtree(outpath)
    os.mkdir(outpath)

for f in os.listdir(path):
    f2, ext = os.path.splitext(f)
    if f2.endswith("clip_2"):
        shutil.copyfile(os.path.join(path, f), os.path.join(outpath, f))

before running the above code make sure that you remove D:/Snow_new/testout completly在运行上面的代码之前,确保你完全删除了D:/Snow_new/testout

暂无
暂无

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

相关问题 将文件从一个文件夹复制到另一个在 .txt 文件中具有匹配名称的文件夹 - Copy files from one folder to another with matching names in .txt file 将文件从一个文件夹复制到另一个文件夹(而不是覆盖) - Copy files from one folder to other (and not overwrite) 将文本文件中列出的特定文件从源文件夹复制到目标文件夹 - Copy the specific files listed in text file from source folder to Destination folder 递归复制文件夹并更改复制文件的文件夹/文件名 - Recursively copy a folder and change folder/file names of copied files 将具有特定名称的文件从一个文件夹复制到 python 中的另一个文件夹 - Copy file with specific name from one folder to another in python python根据文件名中的文本字符将多个文件从一个文件夹移动到另一个文件夹 - python moving multiple files from one folder to the other based on text characters in file name 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 尝试将具有特定扩展名的文件从源文件夹复制到目标文件夹 - Trying to copy files with a specific extension from source folder to destination folder 显示特定文件夹中的所有文件名 - Display all file names from a specific folder Python:将相同的.csv文件从各个文件夹(每个文件夹有一个.csv文件)复制到一个文件夹中 - Python: Copy identical .csv files from various folders (each folder has one .csv file) into a single folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM