欢迎来到山村网

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

2019-03-02 14:22:30浏览:160 来源:山村网   
核心摘要:  本文实例讲述了python清除指定目录内所有文件中script的方法。分享给大家供大家参考。具体如下:  将脚本存储为stripscrip

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

  将脚本存储为stripscripts.py

  调用语法 : python stripscripts.py

  使用范例 : python stripscripts.py d:myfiles

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 # 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读写ini配置文件方法实例分析

上一篇:

RC4文件加密的python实现方法

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com