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和C/C++交互的几种方法总结
May 11 Python
python实现淘宝秒杀脚本
Jun 23 Python
Python实现二叉搜索树BST的方法示例
Jul 30 Python
python生成requirements.txt的两种方法
Sep 18 Python
Python Celery多队列配置代码实例
Nov 22 Python
python二维键值数组生成转json的例子
Dec 06 Python
pytorch .detach() .detach_() 和 .data用于切断反向传播的实现
Dec 27 Python
Python读取VOC中的xml目标框实例
Mar 10 Python
用python实现学生管理系统
Jul 24 Python
Python函数递归调用实现原理实例解析
Aug 11 Python
使用Python提取文本中含有特定字符串的方法示例
Dec 09 Python
彻底解决pip下载pytorch慢的问题方法
Mar 01 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中include()与require()的区别说明
2010/03/10 PHP
使用Discuz关键词服务器实现PHP中文分词
2014/03/11 PHP
php时间戳转换的示例
2014/03/31 PHP
四种php中webservice实现的简单架构方法及实例
2015/02/03 PHP
laravel7学习之无限级分类的最新实现方法
2020/09/30 PHP
JQueryiframe页面操作父页面中的元素与方法(实例讲解)
2013/11/19 Javascript
jquery导航制件jquery鼠标经过变色效果示例
2013/12/05 Javascript
Node.js和MongoDB实现简单日志分析系统
2015/04/25 Javascript
jquery任意位置浮动固定层插件用法实例
2015/05/29 Javascript
JS+CSS实现大气的黑色首页导航菜单效果代码
2015/09/10 Javascript
jQuery实现的指纹扫描效果实例(附演示与demo源码下载)
2016/01/26 Javascript
jQuery简单实现页面元素置顶时悬浮效果示例
2016/08/01 Javascript
Javascript动画效果(3)
2016/10/11 Javascript
JS实现JSON.stringify的实例代码讲解
2017/02/07 Javascript
基于jQuery实现手风琴菜单、层级菜单、置顶菜单、无缝滚动效果
2017/07/20 jQuery
用vue的双向绑定简单实现一个todo-list的示例代码
2017/08/03 Javascript
js删除数组中某几项的方法总结
2019/01/16 Javascript
react native 原生模块桥接的简单说明小结
2019/02/26 Javascript
vue基础之使用get、post、jsonp实现交互功能示例
2019/03/12 Javascript
基于ajax实现上传图片代码示例解析
2020/12/03 Javascript
vue-router定义元信息meta操作
2020/12/07 Vue.js
在Python中使用swapCase()方法转换大小写的教程
2015/05/20 Python
python实现简单登陆系统
2018/10/18 Python
基于python的ini配置文件操作工具类
2019/04/24 Python
Python 用matplotlib画以时间日期为x轴的图像
2019/08/06 Python
python实现网站用户名密码自动登录功能
2019/08/09 Python
Python下应用opencv 实现人脸检测功能
2019/10/24 Python
PyTorch中的padding(边缘填充)操作方式
2020/01/03 Python
Windows下Pycharm远程连接虚拟机中Centos下的Python环境(图文教程详解)
2020/03/19 Python
Python批量删除mysql中千万级大量数据的脚本分享
2020/12/03 Python
Farfetch香港官网:汇集全球时尚奢侈品购物平台
2017/11/26 全球购物
大学生自我鉴定
2013/12/16 职场文书
县级文明单位申报材料
2014/05/23 职场文书
企业晚会策划方案
2014/05/29 职场文书
小学先进集体事迹材料
2014/05/31 职场文书
python实现A*寻路算法
2021/06/13 Python