删除指定格式的文件

2015-06-23 0 359
删除指定格式的文件
#!/usr/bin/python

"""
2015.07.02
Clean all the genrated files during compile.             Edited by Grey
"""

import os,re

def CleanAll(file_ext):
	# remove all files in bin
	for root,dirs,files in os.walk('./bin'):
		for each_file in files:
			try:
				os.chdir('./bin')
				os.remove(each_file)
				os.chdir('../')
			except:
				pass
	# remove all object files in source foulder
	for root,dirs,files in os.walk('./'):
		pwd = os.getcwd()
		os.chdir(root)
		object_file_list = [f for f in os.listdir('.') if f.endswith(file_ext)]
		for file in object_file_list:
			os.remove(file)
		os.chdir(pwd)

CleanAll('.o')
CleanAll('.rar')
CleanAll('.xls')
CleanAll('.xlsx')
CleanAll('.tmp')
CleanAll('.txt')
CleanAll('.lnk')
CleanAll('.doc')
CleanAll('.docx')
CleanAll('.ppt')
CleanAll('.pptx')
CleanAll('.bak')
CleanAll('.c')
CleanAll('.h')
CleanAll('.pdf')
print "All the files and all the files in 'bin' have been removed!"

遇见资源网 python 删除指定格式的文件 http://www.ox520.com/15577.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务