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,清除,目录,文件


猜你喜欢
python目标检测IOU的概念与示例
2022-04-19 07:41:25

ASP下标越界错误的解决方法
2008-10-19 17:39:00
python中的hashlib和base64加密模块使用实例
2022-10-19 05:08:23
Python3爬虫中识别图形验证码的实例讲解
2022-10-17 17:11:20

使用pt-kill根据一定的规则来kill连接的方法
2024-01-26 01:18:37
排序与检索
2008-05-18 13:09:00

ubuntu16.04制作vim和python3的开发环境
2022-08-11 00:11:07
Python干货实战之逆向登录世界上最大的游戏平台Stream
2023-05-13 11:25:19

详解用python自制微信机器人,定时发送天气预报
2023-05-12 14:27:29

关于网站地图
2011-01-06 12:14:00

Python pyinstaller库的安装配置教程分享
2023-06-20 02:08:22

如何使用python实现模拟鼠标点击
2022-07-07 21:46:57
JavaScript判断各种浏览器类型及版本
2008-09-29 15:17:00
Python3实现捕获Ctrl+C终止信号
2021-10-17 14:03:55
vue 使用饿了么UI仿写teambition的筛选功能
2024-04-27 16:05:09

聊聊python中令人迷惑的duplicated和drop_duplicates()用法
2022-01-03 19:10:57

深入了解Python 变量作用域
2023-06-08 10:01:15
python从gbff文件中直接提取cds序列
2023-05-29 16:10:35
Django代码性能优化与Pycharm Profile使用详解
2022-06-28 21:13:06

pytorch visdom安装开启及使用方法
2023-11-27 06:39:41
