python清除指定目录内所有文件中script的方法
作者:秋风秋雨 发布时间:2022-04-05 15:59:47
标签: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程序设计有所帮助。
0
投稿
猜你喜欢
- 导语:排版是一门艺术,也是一门技巧。我们每天都能在报纸,书籍等各种媒介上看到排版,或精美,或丑陋。如何能在准确传递信息的同时,又能排出精美的
- 很多时候,用户可能会因为承受不了某一产品中的细节设计而放弃使用该产品,这无疑是让设计人员感很沮丧的事情,因为除去这个细节的问题外,这个产品可
- 测试方法首先使用implode, serialize, json_encode, msgpack_pack创建四个文本文件,用于测试。创建代
- 1. 序言每年淘宝双十一的时候,总是要刷各种各样的浏览页面,收集能量或者喵币或者什么。那既然如此,我就总想着,能否通过Python自动调用的
- 需求最近公司干活,收到一个需求,说是让手动将数据库查出来的信息复制粘贴到excel中,在用excel中写好的公式将指定的两列数据用updat
- 用dicompyler软件打开dicom图像,头文件如图所示:当然也可以直接读取:ds = dicom.read_file('H:\
- 在php中获取数组长度方法很简单,php为我们提供了两个函数可以计算一维数组长度,如count,sizeof都可以直接统计数组长度哦,下面我
- 相同点:可以利用中括号获取元素 s[0]可以的得到单个元素 或 一个元素切片 s[3,7]可以遍历 for x in s可以调用同样的函数获
- python虚拟环境迁移:注意事项:直接将虚拟环境复制到另一台机器,直接执行是会有问题的。那么可以采用以下办法:思路:将机器1虚拟环境下的包
- 如下所示: out = subprocess.getstatusoutput('adb shell pm
- 前言PC Server发展到今天,在性能方面有着长足的进步。64位的CPU在数年前都已经进入到寻常的家用PC之中,更别说是更高端的PC Se
- 今天摸了半天摸出来的,虽然这里没啥人玩ASP,不过也是win7的问题,发发当备份问题1.An error occurre
- 本文实例讲述了PHP实现向关联数组指定的Key之前插入元素的方法。分享给大家供大家参考,具体如下:PHP 关联数组可以通过三种方式插入新元素
- 我们来编写一个,引用时用:<!--#include Virtual="page.inc"-->语句即可:pa
- #python中的函数定义,使用和传参###------------------- 必要参数 -----------------------
- 本文实例为大家分享了python机器学习实现决策树的具体代码,供大家参考,具体内容如下# -*- coding: utf-8 -*-&quo
- 在使用pytorch作为深度学习的框架时,经常会遇到变量variable、张量tensor与矩阵numpy的类型的相互转化的问题,本章结合这
- 最近在研究tensorflow自带的例程speech_command,顺便学习tensorflow的一些基本用法。其中tensorboard
- 查看表空间的名称及大小代码如下:SQL>select t.tablespace_name, round(sum(bytes/(1024
- 网上找了很多。貌似没什么好的解决办法。自己参考了下。把它直接整成一条SQL语句了。不过字段的格式只允许前面或者后面有字符。中间不允许出现字符