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内存管理分析
Apr 08 Python
举例讲解Python中metaclass元类的创建与使用
Jun 30 Python
python使用标准库根据进程名如何获取进程的pid详解
Oct 31 Python
Python yield与实现方法代码分析
Feb 06 Python
python 不以科学计数法输出的方法
Jul 16 Python
对Python中实现两个数的值交换的集中方法详解
Jan 11 Python
对pyqt5中QTabWidget的相关操作详解
Jun 21 Python
python实现鸢尾花三种聚类算法(K-means,AGNES,DBScan)
Jun 27 Python
使用wxpy实现自动发送微信消息功能
Feb 28 Python
python图片指定区域替换img.paste函数的使用
Apr 09 Python
Python json转字典字符方法实例解析
Apr 13 Python
Pytorch之Tensor和Numpy之间的转换的实现方法
Sep 03 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 strcmp使用说明
2010/04/22 PHP
PHP 计算代码执行耗时的代码修正网上普遍错误
2011/05/14 PHP
仿dedecms下拉分页样式修改的thinkphp分页类实例
2014/10/30 PHP
开启PHP Static 关键字之旅模式
2015/11/13 PHP
php socket通信简单实现
2016/11/18 PHP
JavaScript 动态改变图片大小
2009/06/11 Javascript
javascript 简练的几个函数
2009/08/29 Javascript
JavaScript 密码强度判断代码
2009/09/05 Javascript
JavaScript是否可实现多线程  深入理解JavaScript定时机制
2009/12/22 Javascript
JavaScript在多浏览器下for循环的使用方法
2012/11/07 Javascript
jquery动态增加text元素以及删除文本内容实例代码
2013/07/01 Javascript
js设置function参数默认值(适合没有传参情况)
2014/02/24 Javascript
js判断iframe内的网页是否滚动到底部触发事件
2014/03/18 Javascript
js调试系列 断点与动态调试[基础篇]
2014/06/18 Javascript
JS实现网页滚动条感应鼠标变色的方法
2015/02/26 Javascript
js实现点击链接后延迟3秒再跳转的方法
2015/06/05 Javascript
jQuery在线选座位插件seat-charts特效代码分享
2015/08/27 Javascript
基于jQuery滑动杆实现购买日期选择效果
2015/09/15 Javascript
基于react后端渲染模板引擎noox发布使用
2018/01/11 Javascript
详解在vue-cli中使用graphql即vue-apollo的用法
2018/09/08 Javascript
微信小程序使用canvas的画图操作示例
2019/01/18 Javascript
JavaScript深入V8引擎以及编写优化代码的5个技巧
2019/06/24 Javascript
解决使用layui对select append元素无效或者未及时更新的问题
2019/09/18 Javascript
el-table树形表格表单验证(列表生成序号)
2020/05/31 Javascript
Vue插槽_特殊特性slot,slot-scope与指令v-slot说明
2020/09/04 Javascript
python+opencv实现的简单人脸识别代码示例
2017/11/14 Python
在python中安装basemap的教程
2018/09/20 Python
Python实现电视里的5毛特效实例代码详解
2020/05/15 Python
Python实现加密接口测试方法步骤详解
2020/06/05 Python
5个HTML5的常用本地存储方式详解与介绍
2021/03/27 HTML / CSS
主治医师岗位职责
2013/12/10 职场文书
秋收起义观后感
2015/06/11 职场文书
如何写一份成功的商业计划书
2019/06/25 职场文书
2019自荐信该如何写呢?
2019/07/05 职场文书
MySQL sql_mode的使用详解
2021/05/08 MySQL
Element实现动态表格的示例代码
2021/08/02 Javascript