简体   繁体   中英

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. But.jpg and.png file should be not copied and empty folder should also be copied.

Can this be done in python using any lib?

Use shutil for this:

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.

HTH, ferdy

I'd like to add a 'honorable mention', even if I second ferdy on using shutil:

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

from 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM