python清除指定目录内所有文件中script的方法
2015-06-30 00:00
951 查看
本文实例讲述了python清除指定目录内所有文件中script的方法。分享给大家供大家参考。具体如下:
将脚本存储为stripscripts.py
调用语法 : python stripscripts.py <directory>
使用范例 : python stripscripts.py d:\myfiles
# Hello, this is a script written in Python. See http://www.pyhon.org import os,sys,string,re message = """ stripscripts 1.1p - Script stripper This script will walk a directory (and its subdirectories) and disable all scripts (javascript, vbscript...) from .html and .htm files. (The scripts will not be deleted, but simply deactivated, so that you can review them if you like.) Can be usefull for sites you have downloaded with HTTrack or similar tools. No more nosey or buggy scripts in your local html files. Syntax : python %s <directory> Example : python %s d:\myfiles This script is public domain. You can freely reuse it. The author is Sebastien SAUVAGE <sebsauvage at sebsauvage dot net> http://sebsauvage.net More quick & dirty scripts are available at http://sebsauvage.net/python/ """ % ((sys.argv[0], )*2) def stripscripts ( directoryStart ) : os.path.walk( directoryStart, callback, '' ) def callback ( args, directory, files ) : print 'Scanning',directory for fileName in files: if os.path.isfile( os.path.join(directory,fileName) ) : if string.lower(os.path.splitext(fileName)[1]) in ['.html','.htm'] : stripScriptFromHtml ( os.path.join(directory,fileName) ) def stripScriptFromHtml ( filepath ) : print ' Processing',os.path.split(filepath)[1] file = open(filepath, 'rb') html = file.read() file.close() regexp = re.compile(r'<script.*?>', re.IGNORECASE) html = regexp.sub('<script language="MonthyPythonsScript">',html) file = open(filepath, 'w+') file.write(html) file.close() if len(sys.argv) > 1 : stripscripts( sys.argv[1] ) else: print message
希望本文所述对大家的Python程序设计有所帮助。
相关文章推荐
- python读写ini配置文件方法实例分析
- python获得文件创建时间和修改时间的方法
- python过滤字符串中不属于指定集合中字符的类实例
- python判断一个集合是否包含了另外一个集合中所有项的方法
- python检查序列seq是否含有aset中项的方法
- python简单判断序列是否为空的方法
- python实现爬取千万淘宝商品的方法
- Python实现监控程序执行时间并将其写入日志的方法
- python的keyword模块用法实例分析
- Python项目一:即时标记
- Python中super关键字的作用
- 深刻理解Python中的元类(metaclass)
- Python的getattr(),setattr(),delattr(),hasattr()
- Python 闭包
- 关于Python的super用法
- [Python] Python 之 __new__() 方法与实例化
- [深入Python]__new__和__init__
- Python学习笔记 - 迭代器Iterator
- Python学习笔记 - 迭代器Iterator
- Python学习笔记 - 生成器generator