python批量删除文件夹

制作的python程序跑一次就占200多内存在temp下面,关键是还不释放,最开始都没有发现这个问题,知道自己的c盘越来越小才发现问题所在。所以就有了去删除temp下生成的文件

代码如下:

import getpass
import win32api
import time
#终止Everything
import os
import shutil
import stat
# filePath:文件夹路径
user_name = getpass.getuser() # 获取当前用户名
os.system("taskkill /F /IM localServer04.exe")
time.sleep(5)
def delete_file(filePath):
if os.path.exists(filePath):
for fileList in os.walk(filePath):
for name in fileList[2]:
os.chmod(os.path.join(fileList[0], name), stat.S_IWRITE)
os.remove(os.path.join(fileList[0], name))
shutil.rmtree(filePath)
return "delete ok"
else:
return "no filepath"



ff=os.listdir('C:\\Users\\' + user_name + '\\AppData\Local\Temp\\')

oo=[]
try:
for i in ff:
if i.startswith('_MEI'):
oo.append(i)
try:
delete_file('C:\\Users\\' + user_name + '\\AppData\Local\Temp\\' + i)

print("删除成功,删除的文件夹为: "+i)
except:
continue

finally:
win32api.ShellExecute(0, 'open', r'C:\localsever\localServer04.exe', '','',1)

 

posted @ 2018-10-24 16:06  Bestone  阅读(3093)  评论(0编辑  收藏  举报