user475353
user475353

Reputation:

Python Regex on File Read Input

So im reading from a file like so.

f = open("log.txt", 'w+r')
f.read()

So f now has a bunch of lines, but im mainly concerned with it having a number, and then a specific string (For example "COMPLETE" would be the string)

How...exactly would you go about checking this?
I thought it'd be something like:

r.search(['[0-9]*'+,"COMPLETE")

but that doesn't seem to work? Maybe it's my Regex thats wrong (im pretty terrible at it) but basically it just needs to check the Entire String (which is multiple lines and contain's \n's for a Number (specifically 200) and the word COMPLETE (in caps)

edit: For reference here is what the logfile looks like

Using https
Sending install data... DONE
200 COMPLETE
<?xml version="1.0" encoding="UTF-8"?>
<SolutionsRevision version="185"/>

I just need to make sure it says "200" and COMPLETE

Upvotes: 0

Views: 5789

Answers (3)

Andrew Clark
Andrew Clark

Reputation: 208715

You should use Rafe's answer instead of regex to search for "200 COMPLETE" in the file content, but your current code won't work for reading the file, using "w+r" as the mode will truncate your file. You need to do something like this:

f = open("log.txt", "r")
log = f.read()
if "200 COMPLETE" in log:
    # Do something

Upvotes: 1

Rafe Kettler
Rafe Kettler

Reputation: 76985

Regular expressions are overkill here if you're just looking for "200 COMPLETE". Just do this:

if "200 COMPLETE" in log:
    # Do something

Upvotes: 5

manojlds
manojlds

Reputation: 301607

It should be something like

m = r.search('[0-9]+\s+COMPLETE',line)

Upvotes: 1

Related Questions