惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
T
Threatpost
P
Palo Alto Networks Blog
NISL@THU
NISL@THU
O
OpenAI News
Project Zero
Project Zero
G
GRAHAM CLULEY
P
Privacy International News Feed
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
W
WeLiveSecurity
P
Proofpoint News Feed
腾讯CDC
Cloudbric
Cloudbric
S
Secure Thoughts
C
Check Point Blog
博客园 - Franky
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
GbyAI
GbyAI
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
F
Fortinet All Blogs
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
C
Cisco Blogs
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 感動常在

worldpress 新浪微博同步插件0.1版 linux 与window 相互共享命令 linux添加超级管理员用户,修改,删除用户 linux 定时备份数据库 Linux,find 和perl正则替换文件内容、tar解压缩、cp复制、rm删除 - 感動常在 - 博客园 lamp+vsftp +zend optimizer 配置,需要注意的地方 rhel5.4 yum可用华中科技大学的源 http://centos.ustc.edu.cn/ 執行軟件更新 windows下Rails安装MySql驱动的配置 Rails命令大全 rails 用命令scaffold生成數據模型 wndow環境下運行Ruby on Rails window下apache用戶認証 - 感動常在 - 博客园 asp.net試題(五) 收錄PHP試題 - 感動常在 - 博客园 求兩個數的,最大公約數 - 感動常在 - 博客园 asp.net 試題(四) 求一組9位數且不重複(1-9組成),滿足前n位能被n整除,例如:取先二位能被2整除,取先三位能被3整除...取先九位能被9整除。 字符串位移包含 - 感動常在 - 博客园 任意調整CUP成直綫 - 感動常在 - 博客园
python 替换查找工具,闲暇之余写的,不足之处,见凉。
感動常在 · 2012-12-21 · via 博客园 - 感動常在
 1 #-*- coding: UTF-8 -*-
 2 import sys
 3 import string
 4 import time
 5 print sys.version
 6 import re
 7 import os,shutil,platform,datetime
 8 
 9 starttime = datetime.datetime.now()
10 
11 #dir = "E:\\Users\\wang\\Downloads\\templates\\2011"   #Windows
12 dir = "/home/www/blog.26wz.com/templates/2012/abc" #查找所在目录路径  Linux
13 ext = "htm" #查找文件后缀
14 #多行
15 find_str = u"""abc""" #找查的字符串
16 replace_str = u"""中国人""" #替换的字符串口
17 
18 #编码判断
19 try:
20     content.decode('utf-8')
21 except Exception, e:
22     #gbk
23     find_str = find_str.encode('gb2312')
24     replace_str = replace_str.encode('gb2312')
25 else:
26     #utf-8
27     find_str = find_str.encode('utf-8')
28     replace_str = replace_str.encode('utf-8')    
29 
30 i=0
31 for parent,dirnames, filenames in os.walk(dir):
32     for filename in filenames:
33         if ext == filename.rsplit('.',1)[1]:
34             filename_path = os.path.join(parent, filename)
35             content = file(filename_path,'r').read()
36 
37             if content.find(find_str) > -1 :
38                 i+=1
39                 #复制文件备份
40                 sysstr = platform.system()
41                 if(sysstr == "Windows"):
42                     src = os.getcwd() + filename_path.replace(dir.rsplit('\\',1)[0],'')
43                     targetDir =  src.rsplit('\\',1)[0]
44                 elif(sysstr == "Linux"):
45                     src = os.getcwd() + filename_path.replace(dir.rsplit('/',1)[0],'')
46                     targetDir =  src.rsplit('/',1)[0]
47                 else:
48                     exit()
49                     
50                 if (os.path.exists(targetDir) == False):
51                     os.makedirs(targetDir)
52                     shutil.copy(filename_path, src)        
53             
54                 content = content.replace(find_str,replace_str)
55                 file_write = open(filename_path, 'w')
56                 file_write.write(content)
57                 file_write.close
58             
59                 print filename_path
60 
61 
62             
63 print "file count:" + str(i) #打印出文件替换的个数
64   
65 
66 endtime = datetime.datetime.now()
67 print str((endtime - starttime).seconds) + ' sencond' #执行时间

说明:

一、支持Windows、Linux平台。

二、暂时不支持多行替换,还没有找到好的方法以,如果哪位网友想出,麻烦告知。

三、当模板文件众多,需要替换字符时,挺管用的,用过一些替换工具(如:TextCrawler、等),发现对gb2312  utf-8编码不是很友好。

四、QQ:271059875,技术码农,有时间可交流。