2014-12-22 37 views
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) 
+0

我的建議是,首先獲得所有文件名列表中,然後遍歷該列表,在打開每個文件,然後使用正則表達式在文件的給定內容中找到所需的模式,如果找不到模式,則跳到列表中的下一個文件名,等等直到到達列表的末尾,這很簡單,只需要7個-8行代碼,這有道理嗎? – ZdaR

回答

0
import os 
#import re -- This module can be used for more advanced processings 

path = '/home/anmol/Desktop/test/testing/'#path of sample files 

files = [fiile for fiile in os.listdir(path) if '~' not in fiile] 
#this generates a list with the non-hidden files, hence using '~' 
#os.listdir() returns a list of all the files in a given directory. 

#os.listdir() = ['file 4.txt', 'file 5.txt', 'file 4.txt~', 'file 5.txt~', 
#'file2.txt', 'file1.txt~', 'file1.txt', 'file3.txt', 'file3.txt~', 'file2.txt~'] 

# After using '~' not in fiile 
# files = ['file 4.txt', 'file 5.txt', 'file2.txt', 'file1.txt', 'file3.txt'] 

for file_name in files: 
    document = open(path+file_name).read() #returns a string 
    refined_document = " ".join(document.split("\n")) # replaces \n with a space 
    if "intermediate" in refined_document or "depot" in refined_document: 
     #main working condition 
     print "The required word is found under :",file_name 

文件的內容:

FILE1.TXT

Hi this is the 
program and it doesnpt includes 
the required word! 

Thanks!! 

FILE2.TXT

Hi!, This is the second 
file and it does contains the 
word intermediate 

Thanks!! 

file3.txt

Hello!! This is another file 
and it also contains a 
word depot 
hahahahah 

Thanks!! 

file4.txt

Hi This is a file 
which contains both of them 
as itermediadte of a depot 
stands a human 

Thanks!! 

file5.txt

Hello , 
This file again contains garbage... 
@#*$#YHGTGYBFDSW#$%^&*Q%EWFYB 
syasftyaghdjasbv$^%&*OUSa 
dsadhgvtgw67BR^&^(D87O D 
adas dasjdhashdbs 89&^g 

Thanks!! 
+0

這是執行此操作最常用的方法,您可能希望處理一些其他類型的文件而不是僅處理文本文件,在這種情況下,您可以相應地要求進行任何更改。 – ZdaR

相關問題