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

