实用:python中copy文件和目录

拷贝a目录,但是后缀为txt的不拷贝

import shutil
def fn(src,names):
    s =set(filter(lambda x:x.endswith('txt'),names))
    return s
shutil.copytree('a','e',ignore=fn)

运行结果:

'e'

验证结果:
!tree e

e
|-- a1
|   `-- test2.tar
`-- test.tar
    `-- test.tar

2 directories, 2 files

!tree a

a
|-- a1
|   |-- test2.tar
|   `-- testa1.txt
|-- testa.txt
`-- test.tar
    `-- test.tar

2 directories, 4 files

递归删除:(如同:rm -rf 慎用)

shutil.rmtree('a')

你可能感兴趣的:(python,Python学习记录,copy,python,目录拷贝)