如何将特定文件扩展名的文件复制到我的python(2.5版)脚本文件夹? [英] How do I copy files with specific file extension to a folder in my python (version 2.5) script?

查看:192
本文介绍了如何将特定文件扩展名的文件复制到我的python(2.5版)脚本文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将具有特定文件扩展名的文件复制到新文件夹。我有一个想法如何使用 os.walk 但具体我怎么去使用它?我正在搜索的文件具有特定的文件扩展名只有一个文件夹(此文件夹有2个子目录,但我正在寻找的文件将永远不会在这两个子目录中找到,所以我不需要在这些子目录中搜索) 。提前感谢。

I'd like to copy the files that have a specific file extension to a new folder. I have an idea how to use os.walk but specifically how would I go about using that? I'm searching for the files with a specific file extension in only one folder (this folder has 2 subdirectories but the files I'm looking for will never be found in these 2 subdirectories so I don't need to search in these subdirectories). Thanks in advance.

推荐答案

import glob, os, shutil

files = glob.iglob(os.path.join(source_dir, "*.ext"))
for file in files:
    if os.path.isfile(file):
        shutil.copy2(file, dest_dir)

阅读文档的shutil模块选择适合您需要的函数(shutil.copy(),shutil .copy2()或shutil.copyfile())。

Read the documentation of the shutil module to choose the function that fits your needs (shutil.copy(), shutil.copy2() or shutil.copyfile()).

这篇关于如何将特定文件扩展名的文件复制到我的python(2.5版)脚本文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆