复制目录中的所有文件和文件夹,除了 python 中的 .jpg 和 .png 文件

copy all files and folder inside a dir except .jpg and .png files in python

文件夹结构:

rootfoder:
    subfoler1:
        image.jpg
    subfoler2:
        image.png
        text.txt
    subfoler3

我必须将根文件夹中的所有 files/folder 复制到 des 文件夹。 但是.jpg 和.png 文件不应该被复制,空文件夹也应该被复制。

可以使用任何库在 python 中完成吗?

为此使用 shutil:

import shutil

shutil.copytree('/tmp/source', '/tmp/target' , ignore=shutil.ignore_patterns('*.jpg', '*.png'))

shutil 提供了一个 ignore_patterns 函数来在树复制时跳过某些文件。

HTH, 费尔迪

我想添加一个 'honorable mention',即使我第二次使用 shutil:

from dirsync import sync
sync("./Frog", "./Throat", action="sync", ignore=[".*py",".*jpg"], create=True)

来自 https://github.com/tkhyn/dirsync

它似乎是作为 rsync 的替代命令行实用程序编写的,并添加了存储配置文件的选项,但它可以从 python 调用并完成工作。