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 连连看连接算法
Nov 22 Python
用Python实现一个简单的能够上传下载的HTTP服务器
May 05 Python
tensorflow: 查看 tensor详细数值方法
Jun 13 Python
Python实现正则表达式匹配任意的邮箱方法
Dec 20 Python
python3爬虫怎样构建请求header
Dec 23 Python
浅析Python 实现一个自动化翻译和替换的工具
Apr 14 Python
Python基于机器学习方法实现的电影推荐系统实例详解
Jun 25 Python
用Python识别人脸,人种等各种信息
Jul 15 Python
python通过matplotlib生成复合饼图
Feb 06 Python
使用python批量修改XML文件中图像的depth值
Jul 22 Python
Python中基础数据类型 set集合知识点总结
Aug 02 Python
详解OpenCV曝光融合
Apr 29 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数据库备份还原类分享
2014/03/20 PHP
php中heredoc与nowdoc介绍
2014/12/25 PHP
php使用CURL模拟GET与POST向微信接口提交及获取数据的方法
2016/09/23 PHP
php多进程应用场景实例详解
2019/07/22 PHP
简单的js分页脚本
2009/05/21 Javascript
用Javascript 和 CSS 实现脚注(Footnote)效果
2009/09/09 Javascript
js 蒙版进度条(结合图片)
2010/03/10 Javascript
用客户端js实现带省略号的分页
2013/04/27 Javascript
JS遍历数组及打印数组实例分析
2016/01/21 Javascript
jQuery+css3实现转动的正方形效果(附demo源码下载)
2016/01/27 Javascript
js添加绑定事件的方法
2016/05/15 Javascript
浅谈addEventListener和attachEvent的区别
2016/07/14 Javascript
灵活使用数组制作图片切换js实现
2016/07/28 Javascript
聊一聊jQuery插件uploadify使用方法
2016/08/24 Javascript
Vue中的Vux配置指南
2017/12/08 Javascript
angularjs使用gulp-uglify压缩后执行报错的解决方法
2018/03/07 Javascript
vue初始化动画加载的实例
2018/09/01 Javascript
手挽手带你学React之React-router4.x的使用
2019/02/14 Javascript
详解vue中this.$emit()的返回值是什么
2019/04/07 Javascript
vue登录注册实例详解
2019/09/14 Javascript
javascript 函数的暂停和恢复实例详解
2020/04/25 Javascript
python抓取网页中的图片示例
2014/02/28 Python
python批量同步web服务器代码核心程序
2014/09/01 Python
Python数据分析模块pandas用法详解
2019/09/04 Python
Python实现i人事自动打卡的示例代码
2020/01/09 Python
Python-opencv 双线性插值实例
2020/01/17 Python
怎样写好自我评价呢?
2014/02/16 职场文书
出生医学证明书
2014/09/15 职场文书
亮剑观后感
2015/06/05 职场文书
宾馆客房管理制度
2015/08/06 职场文书
2016年党建工作简报
2015/11/26 职场文书
初三英语教学反思
2016/02/15 职场文书
研究生学习计划书应该怎么写?
2019/09/10 职场文书
浅谈MySQL next-key lock 加锁范围
2021/06/07 MySQL
Vue中插槽slot的使用方法与应用场景详析
2021/06/08 Vue.js
Python 实现Mac 屏幕截图详解
2021/10/05 Python