python清除指定目录内所有文件中script的方法


Posted in Python onJune 30, 2015

本文实例讲述了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 相关文章推荐
python发布模块的步骤分享
Feb 21 Python
Python的requests网络编程包使用教程
Jul 11 Python
Python编程之event对象的用法实例分析
Mar 23 Python
Python AES加密模块用法分析
May 22 Python
python+selenium识别验证码并登录的示例代码
Dec 21 Python
python3 http提交json参数并获取返回值的方法
Dec 19 Python
django 自定义过滤器的实现
Feb 26 Python
pandas删除行删除列增加行增加列的实现
Jul 06 Python
使用Python画出小人发射爱心的代码
Nov 23 Python
keras的load_model实现加载含有参数的自定义模型
Jun 22 Python
pycharm 复制代码出现空格的解决方式
Jan 15 Python
python之基数排序的实现
Jul 26 Python
RC4文件加密的python实现方法
Jun 30 #Python
详解Python中的序列化与反序列化的使用
Jun 30 #Python
Python中函数的参数传递与可变长参数介绍
Jun 30 #Python
python实现文件快照加密保护的方法
Jun 30 #Python
Python实现高效求解素数代码实例
Jun 30 #Python
python实现DES加密解密方法实例详解
Jun 30 #Python
python实现的系统实用log类实例
Jun 30 #Python
You might like
PHP迅雷、快车、旋风下载专用链转换代码
2010/06/15 PHP
将时间以距今多久的形式表示,PHP,js双版本
2012/09/25 PHP
linux环境apache多端口配置虚拟主机的方法深入介绍
2013/06/09 PHP
php实现快速排序的三种方法分享
2014/03/12 PHP
ThinkPHP模版中导入CSS和JS文件的方法
2014/11/29 PHP
php并发加锁问题分析与设计代码实例讲解
2021/02/26 PHP
写的htc的数据表格
2007/01/20 Javascript
js实现权限树的更新权限时的全选全消功能
2009/02/17 Javascript
js过滤数组重复元素的方法
2010/09/05 Javascript
jquery post方式传递多个参数值后台以数组的方式进行接收
2013/01/11 Javascript
js屏蔽鼠标键盘(右键/Ctrl+N/Shift+F10/F11/F5刷新/退格键)
2013/01/24 Javascript
使用js判断控件是否获得焦点
2014/01/03 Javascript
jQuery功能函数详解
2015/02/01 Javascript
javascript实现简单的鼠标拖动效果实例
2015/04/10 Javascript
javaScript中push函数用法实例分析
2015/06/08 Javascript
JS实现先显示大图后自动收起显示小图的广告代码
2015/09/04 Javascript
jQuery绑定事件监听bind和移除事件监听unbind用法实例详解
2016/01/19 Javascript
js获取页面引用的css样式表中的属性值方法(推荐)
2016/08/19 Javascript
从零开始学习Node.js系列教程三:图片上传和显示方法示例
2017/04/13 Javascript
如何理解Vue的.sync修饰符的使用
2017/08/17 Javascript
vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
2018/06/13 Javascript
微信小程序实现多选功能
2018/11/04 Javascript
Vue核心概念Action的总结
2019/01/18 Javascript
vue里的data要用return返回的原因浅析
2019/05/28 Javascript
新手如何快速理解js异步编程
2019/06/24 Javascript
python和shell变量互相传递的几种方法
2013/11/20 Python
python中的字典详细介绍
2014/09/18 Python
安装Python的web.py框架并从hello world开始编程
2015/04/25 Python
python训练数据时打乱训练数据与标签的两种方法小结
2018/11/08 Python
Python 实现两个服务器之间文件的上传方法
2019/02/13 Python
Anaconda3+tensorflow2.0.0+PyCharm安装与环境搭建(图文)
2020/02/18 Python
python线程里哪种模块比较适合
2020/08/02 Python
canvas之万花筒效果的简单实现(推荐)
2016/08/16 HTML / CSS
MySQL 存储过程的优缺点分析
2021/05/20 MySQL
动画《新网球王子 U-17 WORLD CUP》希腊队PV公开
2022/04/02 日漫
详解CSS中postion和opacity及cursor的特性
2022/08/14 HTML / CSS