I have a text file named "filename.txt"
content of file:
This is just a text
content to store
in a file.
i have made two python scripts to extract "to" from the text file
my 1st script:
#!/usr/bin/python
import re
f = open("filename.txt","r")
for line in f:
text = re.match(r"content (\S+) store",line)
x = text.group(1)
print x
my 2nd script:
#!/usr/bin/python
import re
f = open("filename.txt","r")
for line in f:
text = re.match(r"content (\S+) store",line)
if text:
x = text.group(1)
print x
2nd script gives the correct output
bash-3.2$ ./script2.py
to
but 1st script gives me an error
bash-3.2$ ./script1.py
Traceback (most recent call last):
File "./script1.py", line 6, in ?
x = text.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
how is that adding an "if" condition gives me the correct output and when i remove it i get an error?