![](/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.