简体   繁体   English

如何将具有特定文件扩展名的文件复制到我的python(2.5版)脚本中的文件夹中?

[英]How do I copy files with specific file extension to a folder in my python (version 2.5) script?

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? 我知道如何使用os.walk但具体如何使用它? 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). 我只在一个文件夹中搜索具有特定文件扩展名的文件(此文件夹有2个子目录,但我在寻找的文件永远不会在这两个子目录中找到,所以我不需要在这些子目录中搜索) 。 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)

Read the documentation of the shutil module to choose the function that fits your needs (shutil.copy(), shutil.copy2() or shutil.copyfile()). 阅读shutil模块的文档 ,选择适合您需求的函数(shutil.copy(),shutil.copy2()或shutil.copyfile())。

If you're not recursing, you don't need walk(). 如果您没有递归,则不需要walk()。

Federico's answer with glob is fine, assuming you aren't going to have any directories called 'something.ext'. 假设你不会有任何名为'something.ext'的目录,Federico对glob的回答很好。 Otherwise try: 否则尝试:

import os, shutil

for basename in os.listdir(srcdir):
    if basename.endswith('.ext'):
        pathname = os.path.join(srcdir, basename)
        if os.path.isfile(pathname):
            shutil.copy2(pathname, dstdir)

Here is a non-recursive version with os.walk : 这是一个os.walk的非递归版本:

import fnmatch, os, shutil

def copyfiles(srcdir, dstdir, filepattern):
    def failed(exc):
        raise exc

    for dirpath, dirs, files in os.walk(srcdir, topdown=True, onerror=failed):
        for file in fnmatch.filter(files, filepattern):
            shutil.copy2(os.path.join(dirpath, file), dstdir)
        break # no recursion

Example: 例:

copyfiles(".", "test", "*.ext")

This will walk a tree with sub-directories. 这将使用子目录遍历树。 You can do an os.path.isfile check to make it a little safer. 您可以执行os.path.isfile检查以使其更安全一些。

for root, dirs, files in os.walk(srcDir):
    for file in files:
        if file[-4:].lower() == '.jpg':
            shutil.copy(os.path.join(root, file), os.path.join(dest, file))

Copy files with extension "extension" from srcDir to dstDir... 将扩展名为“extension”的文件从srcDir复制到dstDir ...

import os, shutil, sys

srcDir = sys.argv[1] 
dstDir = sys.argv[2]
extension = sys.argv[3]

print "Source Dir: ", srcDir, "\n", "Destination Dir: ",dstDir, "\n", "Extension: ", extension

for root, dirs, files in os.walk(srcDir):
    for file_ in files:
        if file_.endswith(extension):
            shutil.copy(os.path.join(root, file_), os.path.join(dstDir, file_))

暂无
暂无

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

相关问题 如何让我的python(2.5版)脚本在文件夹而不是命令行中运行jar文件? - How can I get my python (version 2.5) script to run a jar file inside a folder instead of from command line? 如何复制具有特定扩展名的文件夹目录中的文件,然后将其保存到新文件夹中而不覆盖已复制的文件? - 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? 我如何使用 zip 文件夹的内容 python(版本 2.5)? - How do I zip the contents of a folder using python (version 2.5)? 如何使用python(2.5版)删除特定数量的文件? - How do I remove a specific number of files using python (version 2.5)? 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 在SVN中的目录中搜索具有特定文件扩展名的文件,然后复制到另一个文件夹? - Search directory in SVN for files with specific file extension and copy to another folder? 如何将文件复制到python中的只读文件夹 - How do i copy files to a read-only folder in python 如何在特定文件夹上运行 python 脚本 - How do I run python script on specific folder Python,我如何在文件夹中找到以特定格式结尾的文件 - Python, how do i find files that end with a specific format in a folder 我如何在 python 的特定文件夹中保存.json 文件 - How do i save .json files in specific folder in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM