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




 

posted @ 2021-12-01 11:02  androidyou  阅读(98)  评论(0编辑  收藏  举报