【发布时间】:2014-12-22 09:01:20
【问题描述】:
该程序搜索包含用户定义的字符串的文件,并根据某些匹配词(即中间和仓库)中的信息读取并运行一些脚本,但它已经发现不止一个文件将包含给定但不包含中间字符串等。我的问题是如何编写它以便程序检查中间文件,如果不存在则搜索下一个等等?
下面是当前使用的代码。
#!/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 行代码,这有意义吗?
标签: python linux search file-handling