0
該程序搜索包含用戶定義的字符串的文件,然後讀取並根據某些匹配詞中的信息(即中間庫和倉庫,但它已經發現多個文件將包含給定的字符串,但不是中間等。我的問題是如何寫它,使程序將檢查文件的中間,如果它不存在然後搜索下一個,所以上?搜索匹配文件(如果不可用)轉到下一個文件
以下是當前使用的代碼。
#!/usr/bin/python
from subprocess import Popen, PIPE, call
import sys, traceback
s_REG=raw_input('Please enter Reg number: ')
try:
a = Popen(["grep -l %s /shares/MILKLINK/PPdir/*/*.ini --exclude=\"/shares/MILKLINK/PPdir/*/conRtChp.ini\" " %s_REG], shell=True, stdout = PIPE)
FILE_DIR = a.communicate()[0]
FILE_DIR=FILE_DIR.rstrip('\n')
FILE=open("%s" %FILE_DIR , 'r')
except IOError:
print "REG DOES NOT EXIST PLEASE CHECK .INI FILE: error code U1001\n"
print "Please consult Error Codes and meanings reference by viewing this program\n"
sys.exit(0)
try:
# Read File into array
for LINES in FILE:
if LINES.strip() == '[intermediate]':
break
for LINES in FILE:
if LINES.strip() == '[depotNum]':
break
if LINES.strip() == '[global]':
break
LINES = LINES.rstrip("\n").split(";")
# Remove Blank Lines and whitespace
LINES = filter(None, LINES)
# Ignore any lines that have been commented out
LINES = filter(lambda x: not x.startswith('#'), LINES)
for I in range(len(LINES)):
# Call the CrossRef Processor for each Reg to make sure for no differences
call(["/shares/optiload/prog/utilPRG/indref.sh", "%s" %LINES[I]])
except:
print "Cannot find Crossref creation script: error code U1002"
print "Please consult Error Codes and meanings reference by viewing this program\n"
sys.exit(0)
FILE.close()
try:
call(["/shares/optiload/prog/utilPRG/sortSs.sh"])
except:
print "Cannot find Crossref sort script: error code U1003"
print "Please consult Error Codes and meanings reference by viewing this program\n"
sys.exit(0)
我的建議是,首先獲得所有文件名列表中,然後遍歷該列表,在打開每個文件,然後使用正則表達式在文件的給定內容中找到所需的模式,如果找不到模式,則跳到列表中的下一個文件名,等等直到到達列表的末尾,這很簡單,只需要7個-8行代碼,這有道理嗎? – ZdaR