[英]copy all files and folder inside a dir except .jpg and .png files in python
Folder Structure:文件夹结构:
rootfoder:
subfoler1:
image.jpg
subfoler2:
image.png
text.txt
subfoler3
I have to copy all the files/folder inside root folder to des folder.我必须将根文件夹中的所有文件/文件夹复制到 des 文件夹。 But.jpg and.png file should be not copied and empty folder should also be copied.
但是.jpg和.png文件不能复制,空文件夹也要复制。
Can this be done in python using any lib?这可以使用任何库在 python 中完成吗?
Use shutil for this:为此使用shutil:
import shutil
shutil.copytree('/tmp/source', '/tmp/target' , ignore=shutil.ignore_patterns('*.jpg', '*.png'))
shutil provides an ignore_patterns function to skip certain files on tree copying. shutil 提供了一个 ignore_patterns function 在树复制时跳过某些文件。
HTH, ferdy HTH,费迪
I'd like to add a 'honorable mention', even if I second ferdy on using shutil:我想添加一个“荣誉提及”,即使我第二次使用shutil:
from dirsync import sync
sync("./Frog", "./Throat", action="sync", ignore=[".*py",".*jpg"], create=True)
from https://github.com/tkhyn/dirsync来自https://github.com/tkhyn/dirsync
It seems to have been written as a command line utility alternative to rsync and adds the option to storing a config file, but it is callable from python and does the job.它似乎是作为 rsync 的替代命令行实用程序编写的,并添加了存储配置文件的选项,但它可以从 python 调用并完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.