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 版)脚本中的文件夹中?


- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01