发布于 2015-09-30 04:55:53 | 197 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python教程,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


本文实例讲述了Python删除windows垃圾文件的方法。分享给大家供大家参考。具体如下:

#coding:utf-8
import os
#from glob import glob
if os.name == 'nt':
 if 'HOMEPATH' in os.environ:
 home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
 else:
 home = os.environ['HOMEPATH']
workpath = os.path.join(home,'Local Settings')
#递归删除文件
#里面和下面的函数用try是抛出删除正在使用的零时文件出错
def delfile(path):
 for file in os.listdir(path): 
 if os.path.isfile(os.path.join(path,file)):
  try:
  print "n删除垃圾文件: %s" % (os.path.join(path,file))
  os.remove(os.path.join(path,file))
  except:
  pass
 elif os.path.isdir(os.path.join(path,file)):
  delfile(os.path.join(path,file)) 
 else:
  pass
delfile(os.path.join(workpath,'Temp'))
delfile(os.path.join(workpath,'Temporary Internet Files'))
#删除文件家的时候必须为空文件夹,而且只能从最里层删起
def deldir(pa):
 for i in os.listdir(pa):
 if os.path.isdir(os.path.join(pa,i)):
  if len(os.listdir(os.path.join(pa,i))) > 0:
  deldir(os.path.join(pa,i))
  try:
   os.rmdir(os.path.join(pa,i))
  except:
   pass
  else:
  try:
   print "n删除文件夹 %s" % (os.path.join(pa,i))
   os.rmdir(os.path.join(pa,i))
  except:
   pass
deldir(os.path.join(workpath,'Temp'))
deldir(os.path.join(workpath,'Temporary Internet Files'))
print """
 系统产生的零时垃圾文件清理完毕!
 """
raw_input("请按回车键退出!")



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务