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中的Classes和Metaclasses详解
Apr 02 Python
Python2.7简单连接与操作MySQL的方法
Apr 27 Python
Python数据类型详解(一)字符串
May 08 Python
python实现换位加密算法的示例
Oct 14 Python
python实践项目之监控当前联网状态详情
May 23 Python
python脚本开机自启的实现方法
Jun 28 Python
Django中的FBV和CBV用法详解
Sep 15 Python
python动态视频下载器的实现方法
Sep 16 Python
python实现七段数码管和倒计时效果
Nov 23 Python
Pytorch中的数据集划分&正则化方法
May 27 Python
Python几种酷炫的进度条的方式
Apr 11 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
Drupal7连接多个数据库及常见问题解决
2014/03/02 PHP
php生成静态页面的简单示例
2014/04/17 PHP
PHP中的插件机制原理和实例
2014/07/08 PHP
PHP解析RSS的方法
2015/03/05 PHP
PHP CURL实现模拟登陆并上传文件操作示例
2020/01/02 PHP
同一个表单 根据要求递交到不同页面的实现方法小结
2009/08/05 Javascript
Dom 结点创建 基础知识
2011/10/01 Javascript
js 判断文件类型并控制表单提交示例代码
2013/11/14 Javascript
jquery iframe操作详细解析
2013/11/20 Javascript
关于onchange事件在IE和FF下的表现及解决方法
2014/03/08 Javascript
Nodejs+express+html5 实现拖拽上传
2014/08/08 NodeJs
js获取本机操作系统类型的两种方法
2015/12/19 Javascript
jQuery与Ajax以及序列化
2016/02/01 Javascript
AngularJS实现标签页的两种方式
2016/09/05 Javascript
JS实现的简单图片切换功能示例【测试可用】
2017/02/14 Javascript
javascript基础进阶_深入剖析执行环境及作用域链
2017/09/05 Javascript
ios中视频的最后一桢问题解决
2019/05/14 Javascript
vue3.0 搭建项目总结(详细步骤)
2019/05/20 Javascript
[01:04]DOTA2上海特锦赛现场采访 FreeAgain遭众解说围攻
2016/03/25 DOTA
python 循环while和for in简单实例
2016/08/16 Python
python实现12306登录并保存cookie的方法示例
2019/12/17 Python
Python 线性回归分析以及评价指标详解
2020/04/02 Python
Django设置Postgresql的操作
2020/05/14 Python
医学生个人求职信范文
2013/09/24 职场文书
廉政教育心得体会
2014/01/01 职场文书
《最大的“书”》教学反思
2014/02/14 职场文书
2014年师德师风学习材料
2014/05/16 职场文书
银行服务明星推荐材料
2014/05/29 职场文书
预防传染病方案
2014/06/14 职场文书
学校端午节活动方案
2014/08/23 职场文书
2014年银行信贷员工作总结
2014/12/08 职场文书
辞职信标准格式
2015/02/27 职场文书
实习班主任自我评价
2015/03/11 职场文书
丧事答谢词大全
2015/09/30 职场文书
我的暑假生活作文(五年级)范文
2019/08/07 职场文书
pd.DataFrame中的几种索引变换的实现
2022/06/16 Python