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

作者:秋风秋雨 时间:2022-04-05 15:59:47 

本文实例讲述了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,清除,目录,文件
0
投稿

猜你喜欢

  • Python 多个图同时在不同窗口显示的实现方法

    2022-07-02 01:20:35
  • Anaconda超详细保姆级安装配置教程

    2022-09-06 08:11:50
  • python装饰器代码深入讲解

    2023-08-17 20:59:19
  • aspjpeg 半透明描边的实现函数

    2008-12-17 12:02:00
  • python实现超简单端口转发的方法

    2022-12-25 04:00:53
  • 微信公众平台网页授权获取用户基本信息中授权回调域名设置的变动

    2023-11-14 14:01:31
  • 一文详解go mod依赖管理详情

    2023-07-13 04:35:06
  • pygame游戏之旅 调用按钮实现游戏开始功能

    2023-04-19 06:36:44
  • Python利用Pillow(PIL)库实现验证码图片的全过程

    2022-05-18 21:27:47
  • 解决python2中unicode()函数在python3中报错的问题

    2023-07-15 03:15:12
  • form 元素内的字段 name 不要跟 form 属性名称一致

    2008-10-22 13:25:00
  • pymongo中group by的操作方法教程

    2021-03-23 05:44:40
  • 如何修改MySQL 5.1 data文件夹路径

    2010-10-25 19:58:00
  • laravel接管Dingo-api和默认的错误处理方式

    2023-11-21 23:29:37
  • python实现图书馆研习室自动预约功能

    2022-03-22 02:04:44
  • Python利用scikit-learn实现近邻算法分类的示例详解

    2021-01-09 18:43:44
  • 安装MySQL错误归档处理

    2008-12-22 14:50:00
  • 浅析Python面向对象编程

    2023-11-23 07:52:12
  • echo(),print(),print_r()之间的区别?

    2023-11-15 08:52:42
  • Python识别html主要文本框过程解析

    2023-11-09 01:31:52
  • asp之家 网络编程 m.aspxhome.com