Android开发帮助文档Doc打开速度慢解决_Python篇

因为Doc目录下的html文件里含有访问google的js文件

[html]  view plain copy print ?
  1. <link rel="stylesheet"  
  2. href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">  

[html]  view plain copy print ?
  1. <script src="http://www.google.com/jsapi" type="text/javascript"></script>  
经查的确如此。

由于这两行脚本需在线访问Google,



python代码批量删除

思路:遍历doc或docs目录及子目录,查找所有.html文件,打开这些文件,读取文件内容,替换上边的js内容为空,把修改内容写回文件,结束。

说着很复杂,用python实现真的很简单。

也可直接下载

[python]  view plain copy print ?
  1. import os  
  2. s1 = '''<link rel="stylesheet"
  3. href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">'''  
  4. s2 = '''<script src="http://www.google.com/jsapi" type="text/javascript"></script>'''  
  5. s3 = '''<script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script>'''  
  6. s4 = '''<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script>'''  
  7. for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):  
  8.     for file in files:  
  9.         fd = root + os.sep + file  
  10.         if ".html" in fd:  
  11.             print fd  
  12.             f = open(fd, 'r')  
  13.             s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")  
  14.             f.close()  
  15.             f = open(fd, 'w')  
  16.             f.write(s)  
  17.             f.close()  
  18.           

献丑一条条解释一下,假定我的Android的开发帮助文档在c:\androidsdk\docs下,遍历其下所有目录和文件名只需用os的walk函数即可完成。

[python]  view plain copy print ?
  1. for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):  
walk返回值,当前遍历的目录root、其下有哪些子目录dirs、有哪些文件files,重要的是递归的遍历指定目录C:\AndroidSdk\docs。

去掉html文件里两条影响速度的js,得先有html文件,

[python]  view plain copy print ?
  1. for file in files:  
  2.     fd = root + os.sep + file  
则构造出了所有文件名,找到(匹配).html文件只需这样

[python]  view plain copy print ?
  1. if ".html" in fd:  
  2.     print fd  
接下来就是干掉影显示(速度)的js了,替换文件里相应内容为空即可。

[python]  view plain copy print ?
  1. f = open(fd, 'r')  
  2.             s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")  
  3.             f.close()  
  4.             f = open(fd, 'w')  
  5.             f.write(s)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值