Python之文件处理-递归删除特定文件

Python之文件处理-递归删除特定文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os


def delete_particular_file(top):
    for root, dirs, files in os.walk(top, topdown=False):
        for file_name in files:
            if file_name.split('.')[-1] == 'ipynb':
                delete_file_name = os.path.join(root, file_name)
                os.remove(delete_file_name)
                print(f'{delete_file_name} done...')
        # for name in dirs:
        #     os.rmdir(os.path.join(root, name))


if __name__ == '__main__':
    top = '/Users/mac/Desktop'
    delete_particular_file(top)
posted @ 2019-05-07 15:31  B站-水论文的程序猿  阅读(744)  评论(0编辑  收藏  举报