實(shí)例如下:
#! /usr/bin/python# -*- coding: utf-8 -*-import osdef 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 eif __name__ == '__main__': dirname = '/home/xx/log' del_dir_tree(dirname)
以上這篇python遞歸刪除指定目錄及其所有內(nèi)容的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
|
新聞熱點(diǎn)
疑難解答
圖片精選