美文网首页
Python通过win32api递归遍历目录删除指定文件的代码

Python通过win32api递归遍历目录删除指定文件的代码

作者: gougoude | 来源:发表于2021-11-24 00:07 被阅读0次

如下的内容是关于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

相关文章

网友评论

      本文标题:Python通过win32api递归遍历目录删除指定文件的代码

      本文链接:https://www.haomeiwen.com/subject/vbngtrtx.html