您好,欢迎来到测品娱乐。
搜索
您的当前位置:首页python清除指定目录内所有文件中script的方法

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

来源:测品娱乐


本文实例讲述了python清除指定目录内所有文件中script的方法。分享给大家供大家参考。具体如下:

将脚本存储为stripscripts.py
调用语法 : python stripscripts.py
使用范例 : 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 
 Example : python %s d:\myfiles
 This script is public domain. You can freely reuse it.
 The author is
 Sebastien SAUVAGE
 
 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'', re.IGNORECASE)
 html = regexp.sub('

希望本文所述对大家的Python程序设计有所帮助。

Copyright © 2019- cepb.cn 版权所有 湘ICP备2022005869号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务