How do I copy files with specific file extension to a folder in my python (version 2.5) script?(如何将具有特定文件扩展名的文件复制到我的 python(2.5 版)脚本中的文件夹中?)
问题描述
我想将具有特定文件扩展名的文件复制到新文件夹.我知道如何使用 os.walk 但具体来说我将如何使用它?我只在一个文件夹中搜索具有特定文件扩展名的文件(此文件夹有 2 个子目录,但我要查找的文件永远不会在这 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 版)脚本中的文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将具有特定文件扩展名的文件复制到我的 python(2.5 版)脚本中的文件夹中?
基础教程推荐
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
