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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容