Home >Backend Development >Python Tutorial >python遍历文件夹并删除特定格式文件的示例

python遍历文件夹并删除特定格式文件的示例

WBOY
WBOYOriginal
2016-06-16 08:45:021395browse

复制代码 代码如下:

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

import os

def del_files(path):
    for root , dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".tmp"):
                os.remove(os.path.join(root, name))
  print ("Delete File: " + os.path.join(root, name))

# test
if __name__ == "__main__":
    path = '/tmp'
    del_files(path)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn