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

yipeiwu_com5年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

pycharm 2019 最新激活方式(pycharm破解、激活)

pycharm 2019 最新激活方式(pycharm破解、激活)

PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测...

python读取word文档的方法

本文实例讲述了python读取word文档的方法。分享给大家供大家参考。具体如下: 首先下载安装win32com from win32com import client as wc...

Python探索之自定义实现线程池

为什么需要线程池呢?         设想一下,如果我们使用有任务就开启一个子线程处理,处理完成后,销毁子线程或等...

实例讲解Python中浮点型的基本内容

1.浮点数的介绍 float(浮点型)是Python基本数据类型中的一种,Python的浮点数类似数学中的小数和C语言中的double类型; 2.浮点型的运算 浮点数和整数在计算机内部存...

Python面向对象原理与基础语法详解

Python面向对象原理与基础语法详解

本文实例讲述了Python面向对象原理与基础语法。分享给大家供大家参考,具体如下: 目标 dir 内置函数 定义简单的类(只包含方法) 方法中的 self 参数 初始化方法 内置方法和属...