Scroll search string until python string is found

I thought it would be easy, after 3 hours of searching, trial and error, it seems not easy.

All I want to do is search the string until the string is found. I am looking for a log file for a line that appears when a condition occurs, for example. when the line "function test 1"appears in the log, I need to find it, and then execute another function.

Finding is not a problem, the problem is that the loop has not yet been found.

It perfectly finds:

for line in open(WebPath + SmokeTest): #these are variables I use to construct the path
    if 'readfalseloop2' in line:
            print True
            f = open(WebPath + SmokeTest,'a')
            f.write('<font color= "#347C2C">readfalseloop2</font><br />')
            f.close()
            break
    else:
        print False

I want to do this until a word is found. Ideally, I would like to include this in several functions, I do not want a separate def at this stage.

- . python, ubuntu.

+3
3

Andrew. /else .

, , IMO

from time import sleep

with open(WebPath + SmokeTest,'a+') as f:
    while True:
        if 'readfalseloop2' in f.read():
            f.seek(0,1)
            f.write('\n<font color= "#347C2C">readfalseloop2</font><br />')
            print True
            break
        print '~',
        f.seek(0,0)
        sleep(2)

, . . ,

<font color= "#347C2C">readfalseloop2</font><br />

, Windows .

.

f.read() f,

<font color= "#347C2C">readfalseloop2</font><br />

.

, . , f.seek(0,1) , .

f.seek(0,1) " 0 "; , , , : 'a'. , f.seek(0,0), .

;

, 'readfalseloop2' f.read() False, f.seek(0,0 ) .

.

: , , utf-8, utf-8 , . , utf-8

.


:

from time import sleep

with open(WebPath + SmokeTest,'r+') as f:
    while not 'readfalseloop2' in f.read():
        print '~',
        f.seek(0,0)
        sleep(2)

    f.seek(0,1)
    f.write('\n<font color= "#347C2C">readfalseloop2</font><br />')
    print 'True'

from time import sleep

with open(WebPath + SmokeTest,'r') as f, open(WebPath + SmokeTest,'a') as g:
    while not 'readfalseloop2' in f.read():
        print '~',
        f.seek(0,0)
        sleep(2)

    g.write('\n<font color= "#347C2C">readfalseloop2</font><br />')
    print 'True'

8 . Python -

+4

, . , , , , , , .

​​:

import re

r = re.compile("readflaseloop2")
in_fn = WebPath + inputName
outf = open(in_fn + ".log","a")
count = 0
for line in open(in_fn):
    m = r.search(line)
    if m:
    if count==0:
            print True
        outf.write("<font color='#347C2C'>%s</font><br>\n" % m.group(0))
        outf.flush()
        count += 1
if count==0:
    print False
+1

vy32, , , ( , ).

, , , . , , .

, , , EOF. , , , .

There are some good answers to this question: How do I view a file for changes?

Writing to the same file will still be a potential problem, especially if another process is open for recording (even if you get parallel access to the work, it will be difficult to force the monitoring program to ignore its own records without risking the absence of a record from the controlled application).

An example of using a line iterator in a file with a .deque collection to store contextual information when using all the I / O optimizations built into the line iterator:

context = deque(maxlen=10) # Remember most recent 10 lines
for line in f:
    # Process the current line
    context.append(line)
+1
source

Source: https://habr.com/ru/post/1789136/


All Articles