#!/usr/bin/python
# coding:utf8
import os
import re
import string
key_word_list = #關鍵字列表
***_log = os.getcwd() + "/" + '***.log' #儲存的日誌
# 日誌 儲存乙個檔案中
def logtofile(msg):
try:
fp = open(search_key_log, 'a+')
except ioerror:
self.warn("[warning] " + ***_log + " can't be created")
fp.close()
return -1
except exception as e:
self.warn("general error when opening " +
***_log + " " + str(e))
fp.close()
return -1
fp.write(msg + "\n")
fp.close()
return 1
# 判斷檔案中是否包含關鍵字,是則將檔案路徑列印出來
def is_file_contain_word(file_list):
logflag = 0
for _file in file_list:
try:
with open(_file, 'r') as fd:
count = 0
for i in range(0, len(key_word_list), 1):
query_word = key_word_list[i]
while true:
line = fd.readline()
if not line:
break
count += 1
line = line.strip()
if not len(line) or line.startswith('//'):
continue
for i in range(0, len(key_word_list), 1):
query_word = key_word_list[i]
if query_word in line:
info = "find keyword:%s , in file:%s, the line number is %d" %(query_word,_file, count)
logtofile(info)
logflag = 1
except ioerror:
print("%s can not open" %(_file))
if logflag == 0:
print("no key words found in this directory")
else:
print("using cat ***.log to check result at current directory")
print("finish searching.")
return 1
# 返回指定目錄的所有檔案(包含子目錄的檔案)
def get_all_file(floder_path):
file_list =
if floder_path is none:
raise exception("floder_path is none")
for dirpath, dirnames, filenames in os.walk(floder_path):
for name in filenames:
return file_list
if __name__ == '__main__':
basedir = raw_input("please input the directory:")
is_file_contain_word(get_all_file(basedir))
bin目錄下hadoop指令碼使用
一 hadoop指令碼使用方法 hadoop config confdir command config 用於設定hadoop配置檔案目錄,預設目錄為 conf command 命令 二 具體操作 1.hdfs管理命令 bin hadoop command genericoptions comman...
Shell指令碼統計當前目錄下目錄和檔案的數量
linux下如何統計當前目錄下檔案有多少個,目錄又有多少個呢?下面用shell寫乙個指令碼,放置在當前目錄下,執行即可。複製 如下 bin bash 指令碼名稱 dir 定義乙個函式fun directory fun directory jjuzvrcvnbsp 呼叫函式 fun directory...
C 遞迴搜尋指定目錄下的檔案或目錄
實現說明 仍然是基於對directory類的幾個方法的封裝進行實現,只是沒有使用它們的searchpattern和searchoption功能 將匹配模式由windows的萬用字元?改為正則匹配。一是讓匹配更強大,二是要實現?匹配還得做額外工作,沒必要 匹配模式並沒有預設新增首尾限定 即 abc 將...