Python通過win32api遞歸遍歷目錄刪除指定文件的代碼

如下的內容是關於Python通過win32api遞歸遍歷目錄刪除指定文件的內容。

import win32con
import win32api
import os

def del_dir(self,path):
    for file in os.listdir(path):
        file_or_dir = os.path.join(path,file)
        if os.path.isdir(file_or_dir) and not os.path.islink(file_or_dir):
            del_dir(file_or_dir) #it's a directory reucursive call to function again
        else:
            try:
                os.remove(file_or_dir) #it's a file, delete it
            except:
                #probably failed because it is not a normal file
                win32api.SetFileAttributes(file_or_dir, win32con.FILE_ATTRIBUTE_NORMAL)
                os.remove(file_or_dir) #it's a file, delete it
        os.rmdir(path) #delete the directory here
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章