python清除指定目錄內(nèi)所有文件中script的方法
本文實例講述了python清除指定目錄內(nèi)所有文件中script的方法。分享給大家供大家參考。具體如下:
將腳本存儲為stripscripts.py
調(diào)用語法 : 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程序設(shè)計有所幫助。
相關(guān)文章
Python3使用tesserocr識別字母數(shù)字驗證碼的實現(xiàn)
這篇文章主要介紹了Python3使用tesserocr識別字母數(shù)字驗證碼的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
python2 與python3的print區(qū)別小結(jié)
這篇文章主要介紹了python2 與python3的print區(qū)別小結(jié),需要的朋友可以參考下2018-01-01
Python實現(xiàn)windows下模擬按鍵和鼠標(biāo)點擊的方法
這篇文章主要介紹了Python實現(xiàn)windows下模擬按鍵和鼠標(biāo)點擊的方法,涉及Python模擬實現(xiàn)鼠標(biāo)及鍵盤事件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
Python替換字符串replace()函數(shù)使用方法詳解
Python中的replace()方法是把字符串中的old(舊字符串)替換成new(新字符串),如果指定第三個參數(shù)max,則替換次數(shù)不超過max次(將舊的字符串用心的字符串替換不超過max次,本文就給大家講講Python replace()函數(shù)的使用方法,需要的朋友可以參考下2023-07-07
python?字符串常用方法超詳細(xì)梳理總結(jié)
字符串是Python中基本的數(shù)據(jù)類型,幾乎在每個Python程序中都會使用到它。本文為大家總結(jié)了Python中必備的31個字符串方法,需要的可以參考一下2022-03-03
Python實現(xiàn)的刪除重復(fù)文件或圖片功能示例【去重】
這篇文章主要介紹了Python實現(xiàn)的刪除重復(fù)文件或圖片功能,結(jié)合實例形式分析了Python基于os與hashlib模塊針對文件的讀取、hash計算及重復(fù)性判定等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04

