该程序搜索包含用户定义的字符串的文件,然后读取并根据某些匹配单词中的信息运行一些脚本,即中间和库,但它已经发现多个文件将包含给定的字符串,但不包含中间等。我的问题是如何编写它以便程序将检查文件是否为中间文件,如果它不存在则再搜索下一个等等?
以下是当前使用的代码。
#!/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 :(得分: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!!