![](/img/trans.png)
[英]Python: Converting all files in a folder to JPG, but only JPG and PNG should be considered
[英]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
我必须将根文件夹中的所有文件/文件夹复制到 des 文件夹。 但是.jpg和.png文件不能复制,空文件夹也要复制。
这可以使用任何库在 python 中完成吗?
为此使用shutil:
import shutil
shutil.copytree('/tmp/source', '/tmp/target' , ignore=shutil.ignore_patterns('*.jpg', '*.png'))
shutil 提供了一个 ignore_patterns function 在树复制时跳过某些文件。
HTH,费迪
我想添加一个“荣誉提及”,即使我第二次使用shutil:
from dirsync import sync
sync("./Frog", "./Throat", action="sync", ignore=[".*py",".*jpg"], create=True)
来自https://github.com/tkhyn/dirsync
它似乎是作为 rsync 的替代命令行实用程序编写的,并添加了存储配置文件的选项,但它可以从 python 调用并完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.