今天由于要解决一个前台问题,主页的背景图片不知道跑哪里去了。
然后我在目录里找到那个图片的名字。
由于我的前端知识太差了,忘记到对应的css文件去找那个图片路径。
直接根据这几天的python学习,写了一个搜索文件。
结果在css文件中发现被注释掉了。。
最后感慨到Python神马的简直太方便了。
安装python后,直接在cmd里敲命令
python search.py 目录 要搜索的字符串
小程序的思路就是,根据目录递归文件,然后搜索出文件,整出一个list
然后循环打开list里的文件,搜索内容
废话不多说了。贴代码
# -*- coding: cp936 -*-
import os,sys
def search_string(filename,string):
with open(filename) as file:
lines=file.readlines()
file.close()
#print filename
for i in lines:
i=i.strip()
if i.find(string)!=-1:
print filename
def get_process_files(root_dir):
"""process all files in directory"""
cur_dir=os.path.abspath(root_dir)
file_list=os.listdir(cur_dir)
process_list=[]
for file in file_list:
fullfile=cur_dir+"\\"+file
if os.path.isfile(fullfile):
process_list.append(fullfile)
elif os.path.isdir(fullfile):
dir_extra_list=get_process_files(fullfile)
if len(dir_extra_list)!=0:
for x in dir_extra_list:
process_list.append(x)
return process_list
def count_files(root_dir,string):
process_list=get_process_files(root_dir)
for files in process_list:
search_string(files,string)
if __name__=='__main__':
root_dir=sys.argv[1]#目录
string=sys.argv[2]#要搜索的字符串
count_files(root_dir,string)