1

I have a directory /backup/servers in which I have file as below:

URF
VPF
XHF

And I have a another file /SUPPORT/data.txt which is having content as below :

URF:Active:26-JAN-13
VPF:Active:26-JAN-13
XHF:Active:26-JAN-13
GSA:Active:26-JAN-13
HDKK:Active:26-JAN-13

I am listing /backup/servers directory and writing into wmc.txt file and want to match the content of this file to /SUPPORT/data.txt file and print matching lines of /SUPPORT/data.txt file.

For this, I have written the script below but this is not giving me any output. Can you please help me to get this accomplished.

#!/usr/local/bin/python2.7
import os
import re

DATA="/SUPPORT/data.txt"
path="/backup/servers"

fd = os.listdir(path)
p = r'wmc.txt'
p1 = r'wmc1.txt'
fh = open(p, 'w+')

for i in fd[-15:]:
    if re.search("(.*)-MC(.*)",i):
        rslt=i.replace("-MC","")
        fh.write(rslt)
        fh.write("\n")
        fl=open(DATA,'r')

        for line1 in fl:
            for line2 in fh:
                if re.match("(.*)line2(.*)",line1):
                    print line1
fh.close()
fl.close()

Current Code:

path="/backup/servers"
DATA="SUPPORT/data.txt"
fd=os.listdir(path)
for i in fd[-15:]:
        if re.match("(.*)-MC(.*)",i):
                rslt=i.replace("-MC","")
                fh=open("wmc.txt",'w+')
                fh.write(rslt)
                fl=open(DATA,'r')
                for line1 in fl:
                        fh.seek(0,0)
                        for line2 in fh:
                                if re.search(".*%s.*"%(line2),line1):
                                #if re.search(line2,line1):
                                        print line1
                                        break
Srinivas
  • 13
  • 4

1 Answers1

0

Basically, as you write, your file pointer keeps moving to the last character that is written.

Say for example, I have a file test.py

>>> f = open("test.py","w+") #this creates file
>>> f.write("hello") #write contents to file
>>> f.tell() #check for file pointer - since 5 character are written,
5 
>>> [i for i in f] #on reading lines from file starts reading from where the file pointer is, therefore prints null.
[]

This is the problem that you are facing. Since you write and simultaneously read from the same file you tend to access the file pointer that is at the EOF and hence end up looping through empty list!

Therefore, using f.seek should move the file pointer to the desired position(reference)!

Say for example,

>>> import os
>>> f.seek(0,os.SEEK_END)
>>> f.tell()
5
>>> [i for i in f]
[]
>>> f.seek(0,os.SEEK_SET)
>>> f.tell()
0
>>> [i for i in f]
['hello']

So for your problem,

EDIT Its not just with the file pointer, but also, your regex. The regex that you have currently checks for the text 'line2' in line1! Hence they wont be any match!

You have concatenate line 2 with the (.*) in regular expression. That is like ".*"+line2+".*". or using string formatting as ".*%s.*"%(line2)

import os
for i in fd[-15:]:
    if re.search("(.*)-MC(.*)",i):
        rslt=i.replace("-MC","")
        fh.seek(0,os.SEEK_END) # append contents to file
        fh.write(rslt)
        fh.write("\n")
        fl=open(DATA,'r')

        for line1 in fl:
            fh.seek(0,os.SEEK_SET) # move file pointer to beginning of file
            for line2 in fh:
                #if re.match("(.*)line2(.*)",line1): should be
                if re.match(".*%s.*"%(line2),line1):
                    print line1

Hope that helps!

Community
  • 1
  • 1
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
  • Thanks for help. I am getting this error for object 'SEEK_END' Traceback (most recent call last): File "du.py", line 13, in ? fh.seek(0,os.SEEK_END) # append contents to file AttributeError: 'module' object has no attribute 'SEEK_END' – Srinivas Mar 14 '17 at 19:01
  • I have changed os.SEEK_END and os.SEEK_SET with magic numbers 2 and 0 respectively. Now not getting any error but also not getting any output. – Srinivas Mar 14 '17 at 19:17
  • Look into the edit! Its not just with the file pointer, but also, your regex that was causing the issue! The regex that you have currently checks for the text 'line2' in line1! Hence therewont be any match! – Keerthana Prabhakaran Mar 15 '17 at 02:44
  • Looks like still the issue is with file pointer. Please have a look in "Case-2 Code" example. I am not getting of file 'Test.txt' whether I use f.seek(0,0) or f.seek(0,2). And if I try to use SEEK_END and SEEK_SET, getting error which I have mentioned in first comment. – Srinivas Mar 15 '17 at 05:37
  • f.seek(0,2) you are still at the end of the file! Therefore use f.seek(0,0) before reading to start reading from the beginning! – Keerthana Prabhakaran Mar 15 '17 at 05:48
  • The second param, 0: means your reference point is the beginning of the file 1: means your reference point is the current file position 2: means your reference point is the end of the file – Keerthana Prabhakaran Mar 15 '17 at 05:50
  • Thanks for all your help. I have edited the code again. Can you please check my "current code". Now its giving all content from '/SUPPORT/data.txt' file instead of giving matching content. – Srinivas Mar 15 '17 at 10:43
  • you should print line2 instead of line1! that is because, line2 is the string in regex that is matched with a bigger string line1 – Keerthana Prabhakaran Mar 15 '17 at 17:12