【问题标题】:Search file for match, if not available go to next file搜索匹配的文件,如果不可用,转到下一个文件
【发布时间】: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


【解决方案1】:
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!!

【讨论】:

  • 这是最通用的方法,您可能想要处理一些其他类型的文件而不仅仅是文本文件,在这种情况下您可以要求相应的任何更改。
猜你喜欢
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2015-05-03
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 2016-09-11
相关资源
最近更新 更多