使用ref编号搜索文件

时间:2014-12-09 08:54:03

标签: python linux find string-matching

我正在尝试编写一个程序,当系统提示用户输入参考编号时,程序会搜索包含该编号的文件。首先要做的事情很少:

所有文件都以.dpt扩展名结尾,如下所示:

PX12RUJ 
PX12RUR
PX12RUV
#PX12RUU
PX12WLJ
#PX12WLL
PX12WLK
PX12RUW
WN14YGV
WN14YGY

找到文件后,我需要将其内容读入数组,忽略所有以'#'开头的行。

编辑:搜索代码但输出为空,linux中的grep返回正确的文件test.dpt

#!/usr/bin/python

import subprocess

a = subprocess.Popen(("grep -l PX12WLK /shares/MILKLINK/PPdir/*/*.dpt"),shell=True, stdout = subprocess.PIPE)
output = a.communicate()[0]
print output

编辑2:最后为感兴趣的人对其进行排序&#39>如何:

#!/usr/bin/python

from subprocess import Popen, PIPE, call


s_REG=raw_input('Please enter Reg number: ')

a = Popen(["grep -l %s /shares/MILKLINK/PPdir/*/*.dpt" %s_REG],shell=True, stdout = PIPE)
FILE_DIR = a.communicate()[0]
FILE_DIR=FILE_DIR.rstrip('\n')

FILE=open("%s" %FILE_DIR , 'r')
# Read File into array
LINES = FILE.read().split('\n')
# 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)
FILE.close()

# Call the CrossRef Processor for each Reg to make sure for no differences
for I in range(len(LINES)):
   call(["/shares/optiload/prog/indref.sh", "%s" %LINES[I]]) 

1 个答案:

答案 0 :(得分:1)

#!/usr/bin/python

from subprocess import Popen, PIPE, call


s_REG=raw_input('Please enter Reg number: ')

a = Popen(["grep -l %s /shares/MILKLINK/PPdir/*/*.dpt" %s_REG],shell=True, stdout = PIPE)
FILE_DIR = a.communicate()[0]
FILE_DIR=FILE_DIR.rstrip('\n')

FILE=open("%s" %FILE_DIR , 'r')
# Read File into array
LINES = FILE.read().split('\n')
# 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)
FILE.close()

# Call the CrossRef Processor for each Reg to make sure for no differences
for I in range(len(LINES)):
   call(["/shares/optiload/prog/indref.sh", "%s" %LINES[I]])