0

I'm trying to write a python Script that write regex output (IP Addresses) to a text file. The script will rewrite the file each time it runs. Is there any better way to do it?

import re

pattern = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')

with open('test.txt', 'r') as rf:

    content = rf.read()
    matches = pattern.findall(content)

    open('iters.txt', 'w').close()
    for match in matches:
        with open('iters.txt', 'a') as wf:
            wf.write(match + '\n')
2
  • 1
    Better way ? can you explain what you mean ? Commented Jul 15, 2018 at 11:52
  • Sorry, i mean if there is a shorter way to do it? when i run the script on a big text files it takes long time. I feel like it is not efficient enough. Commented Jul 15, 2018 at 11:59

1 Answer 1

1

I rewrote the code a bit.

  1. I changed the regex to use {3} that way you don't have to repeat the same pattern so many times.
  2. I added os.path.exists to check to see if the file already exists. I think this is what you want, if not just remove that if. If the file already exists it does not write.
  3. I combined the two with's as you seem to be writing to a file and it doesn't make a ton of sense to keep on reopening the file to append a new line.
  4. I renamed pattern to ip_pattern just for readability sake, you can change it back if you want.

Code:

import re, os

ip_pattern = re.compile(r'(?:\d{1,3}\.){3}\d{1,3}')

if not os.path.exists("iters.txt"):
    with open('test.txt', 'r') as rf, open('iters.txt', 'a') as wf:
        content = rf.read()
        matches = ip_pattern.findall(content)
        for match in matches:
            wf.write(match + '\n')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.