欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python遞歸刪除指定目錄及其所有內(nèi)容的方法

 更新時(shí)間:2017年01月13日 08:46:01   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇python遞歸刪除指定目錄及其所有內(nèi)容的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

實(shí)例如下:

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

import os

def del_dir_tree(path):
 ''' 遞歸刪除目錄及其子目錄, 子文件'''
 if os.path.isfile(path):
  try:
   os.remove(path)
  except Exception, e:
   #pass
   print e
 elif os.path.isdir(path):
  for item in os.listdir(path):
   itempath = os.path.join(path, item)
   del_dir_tree(itempath)
  try:
   os.rmdir(path) # 刪除空目錄
  except Exception, e:
   #pass
   print e

if __name__ == '__main__':

 dirname = '/home/xx/log'
 del_dir_tree(dirname)

以上這篇python遞歸刪除指定目錄及其所有內(nèi)容的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論