[Python] python清除指定目录内的所有文件的script →→→→→进入此内容的聊天室

来自 , 2020-11-11, 写在 Python, 查看 103 次.
URL http://www.code666.cn/view/6018df18
  1. # Hello, this is a script written in Python. See http://www.pyhon.org
  2. import os,sys,string,re
  3.  
  4. message = """
  5. stripscripts 1.1p - Script stripper
  6.  
  7. This script will walk a directory (and its subdirectories) and disable
  8. all scripts (javascript, vbscript...) from .html and .htm files.
  9. (The scripts will not be deleted, but simply deactivated, so that
  10.  you can review them if you like.)
  11.  
  12. Can be usefull for sites you have downloaded with HTTrack or similar tools.
  13. No more nosey or buggy scripts in your local html files.
  14.  
  15. Syntax  : python %s <directory>
  16.  
  17. Example : python %s d:\myfiles
  18.  
  19. This script is public domain. You can freely reuse it.
  20. The author is
  21.       Sebastien SAUVAGE
  22.       <sebsauvage at sebsauvage dot net>
  23.       http://sebsauvage.net
  24.  
  25. More quick & dirty scripts are available at http://sebsauvage.net/python/
  26. """ % ((sys.argv[0], )*2)
  27.  
  28. def stripscripts ( directoryStart ) :
  29.     os.path.walk( directoryStart, callback, '' )
  30.  
  31. def callback ( args, directory, files ) :
  32.     print 'Scanning',directory
  33.     for fileName in files:
  34.         if os.path.isfile( os.path.join(directory,fileName) ) :
  35.             if string.lower(os.path.splitext(fileName)[1]) in ['.html','.htm'] :
  36.                 stripScriptFromHtml ( os.path.join(directory,fileName) )
  37.  
  38. def stripScriptFromHtml ( filepath ) :
  39.     print '  Processing',os.path.split(filepath)[1]
  40.     file = open(filepath, 'rb')
  41.     html = file.read()
  42.     file.close()
  43.     regexp = re.compile(r'<script.*?>', re.IGNORECASE)
  44.     html = regexp.sub('<script language="MonthyPythonsScript">',html)
  45.     file = open(filepath, 'w+')
  46.     file.write(html)
  47.     file.close()
  48.  
  49. if len(sys.argv) > 1 :
  50.     stripscripts( sys.argv[1] )
  51. else:
  52.     print message
  53.  
  54. #//python/5171

回复 "python清除指定目录内的所有文件的script"

这儿你可以回复上面这条便签

captcha