the language I am using is python
the problem is: Write a function that takes one argument (a filename). File contains various text lines and occasionally a phone number in them (i.e. not all lines contain phone number). Read given file line by line and search for a phone number (using regular expression) in it, if phone number exists in given line, write this line to phone-number-containing-lines.txt otherwise write this line to plain-lines.txt. As a result, some lines will be in one file and others will be in second file.
this is the code I've come up with:
import re
f1 = open('phonenumber.txt', 'r')
regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
for line in f1:
phone_numbers = regex.findall(line)
for num in phone_numbers:
f = open('phone-number-containing-lines.txt', 'w')
f.writelines(num)
f.close()
f2 = open('phonenumber.txt','r')
searchquery = re.compile(r'^[^\d]*$')
for line in f2:
plain_text = regex.findall(line)
for txt in plain_text:
d = open('plain-lines.txt', 'w')
d.writelines(txt)
d.close()
I don't get any kind of an error but I also just ended up with phone-number-containing-lines.txt only having one of the phone numbers and none of the text from that line, and plain-lines.txt is completely empty